Хорошо, я читаю книгу Copliens C ++ Idioms и пытаюсь запустить примеры из дескриптора / тела в этой книге. После ввода кода я получаю ошибки компиляции:
Вот код классов String и StringRep.
#ifndef _STRINGREP_H_
#define _STRINGREP_H_
#include <stdio.h>
#include <string.h>
class String;
class StringRep {
friend class String;
public:
StringRep() {*(rep = new char[1])='\0';}
StringRep(const StringRep& s) {
::strcpy(rep=new char[::strlen(s.rep)+1], s.rep);
}
~StringRep() { delete [] rep;}
StringRep(const char* s) {
::strcpy(rep=new char[::strlen(s)+1], s);
}
String operator+(const String& s) const {
char *buf = new char[s->length() + length() + 1];
::strcpy(buf, rep);
::strcat (buf, s->rep);
String retval(&buf);
return retval;
}
int length() const { return ::strlen(rep); }
void print() const {::printf("%s\n", rep); }
private:
StringRep(char ** const r) {
rep = *r;
*r = 0;
count = 1;
};
char *rep;
int count;
};
#endif
#ifndef _STRING_H_
#define _STRING_H_
#include <stdio.h>
#include <string.h>
#include "StringRep.h"
class String {
friend class StringRep;
public:
String operator+(const String& s) const {return *p + s;}
StringRep* operator->() const {return p;}
String() {
(p = new StringRep())->count = 1;
}
String (const String &s) { (p=s.p)->count++;}
String(const char* s) {
(p = new StringRep(s))->count = 1;
}
String operator=(const String& s) {
if (--p->count <=0) delete p;
(p = s.p)->count++;
return *this;
}
~String() { if (--p->count <= 0) delete p;; }
private:
String(char **r) {
p = new StringRep(r);
}
StringRep *p;
};
#endif
и main.cc
#include <stdio.h>
#include <string.h>
#include "StringRep.h"
#include "String.h"
int main() {
String a("abcd"), b("efgh");
printf("a is "); a->print();
printf("b is "); b->print();
printf("concat of a+b is "); (a+b)->print();
return 0;
}
Ошибки компиляции;
GNU C++ version 4.1.2 20080704 (Red Hat 4.1.2-44) (x86_64-redhat-linux)
compiled by GNU C version 4.1.2 20080704 (Red Hat 4.1.2-44).
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 2d02d8750f9b337bb19a7dd5b4e2167e
StringRep.h: In member function 'String StringRep::operator+(const String&) const':
StringRep.h:21: error: return type 'struct String' is incomplete
StringRep.h:22: error: base operand of '->' has non-pointer type 'const String'
StringRep.h:24: error: base operand of '->' has non-pointer type 'const String'
StringRep.h:25: error: variable 'String retval' has initializer but incomplete type
String.h: In member function 'String String::operator+(const String&) const':
String.h:13: error: conversion from 'void' to non-scalar type 'String' requested
Я полагаю, что не могу использовать класс String, пока он не будет полностью определен. Изменение
подпись функции
String& operator+(const String& s) const {...
решает первую ошибку, но вызывает ту же ошибку, чтобы показать, где я создаю
новый объект String
String retval(&buf);
Я понимаю, что моя книга - это перепечатка 1994 года, которую я взял. Так может кто-нибудь либо указать мне более новый код (если стиль кодирования C ++ изменился), либо указать, как это исправить?
Спасибо