使用Copliens 1994获取编译错误计算了指针示例代码

时间:2011-07-06 19:51:32

标签: c++ pointers idioms

好的,我正在阅读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 ++编码风格已经改变)或者指出如何解决这个问题吗?

由于

3 个答案:

答案 0 :(得分:2)

您有一个循环引用,因为StringRep需要知道String的完整定义才能在operator+中构建它。我建议不要将所有内容都放在头文件中,而只放在成员函数的声明中,并将定义放在.cpp(或.cc中)文件。那应该解决它。如果类本身和/或函数不是模板,那么也应该如何拆分代码。

答案 1 :(得分:0)

您需要将所有内容的定义和声明分开。如果您确实想将它们放在头文件中,可以按如下方式进行:

<forward declarations, required includes>
<class itself, no functions defined>
<includes for forward declarations that are needed for function bodies>
<function bodies>

这将始终有效。

答案 2 :(得分:0)

当您还在String头文件中包含StringRep头文件时,不能在StringRep头文件中包含String头文件。这会导致循环包含,并会阻止您的代码编译。

将其视为:

StringRep.h loads String.h
  which loads StringRep.h
    which loads String.h
      which loads StringRep.h
        which loads ...

您可以通过将函数声明移动到.cpp文件中并简单地在StringRep.h中声明String类来避免这种情况(您已经这样做了!)。它实际上不会尝试解析对String类的引用,直到它实际编译代码为止,此时循环包含问题将被避免。