好的,我正在阅读Copliens C++惯用法书,并尝试运行书中的handle / body示例。输入代码后,出现编译错误:

这是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++编码样式已更改)还是指出如何解决此问题?

谢谢

最佳答案

由于StringRep需要知道String的完整定义才能在operator+中进行构造,因此您获得了循环引用。我建议不要将所有内容放在头文件中,而应仅将成员函数的声明放到.cpp(或.cc)文件中。那应该解决它。如果类本身和/或函数不是模板,这也应该拆分代码。

关于c++ - 使用Copliens 1994计数指针示例代码遇到编译错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6602082/

10-11 22:33
查看更多