2011-10-31 30 views
0

目前我在书中学习C++,他们有一个关于使用以前创建的名为IntList的类并使用IntListProxy实现它的练习。我的书只是以一个非常简单的例子来谈论代理服务器,所以我很难理解它的语法。我在做什么这个代理错了,我该如何解决它?请记住,IntList已经是.o,我不允许在编译时包含IntList.cpp。 错误:语法错误与代理:我做错了什么?

IntListProxy.cpp: In member function ‘bool IntListProxy::isEmpty()’: 
IntListProxy.cpp:7: error: invalid use of incomplete type ‘struct IntList’ 
IntListProxy.h:5: error: forward declaration of ‘struct IntList’ 
IntListProxy.cpp: In member function ‘IntListProxy* IntListProxy::prepend(int)’: 
IntListProxy.cpp:13: error: invalid use of incomplete type ‘struct IntList’ 
IntListProxy.h:5: error: forward declaration of ‘struct IntList’ 
IntListProxy.cpp: In member function ‘int IntListProxy::head()’: 
IntListProxy.cpp:19: error: invalid use of incomplete type ‘struct IntList’ 
IntListProxy.h:5: error: forward declaration of ‘struct IntList’ 
IntListProxy.cpp: In member function ‘IntListProxy* IntListProxy::tail()’: 
IntListProxy.cpp:25: error: invalid use of incomplete type ‘struct IntList’ 
IntListProxy.h:5: error: forward declaration of ‘struct IntList’ 
IntListProxy.cpp: In member function ‘std::string IntListProxy::toString()’: 
IntListProxy.cpp:31: error: invalid use of incomplete type ‘struct IntList’ 
IntListProxy.h:5: error: forward declaration of ‘struct IntList’ 
IntListProxy.cpp: In member function ‘int IntListProxy::length()’: 
IntListProxy.cpp:37: error: invalid use of incomplete type ‘struct IntList’ 
IntListProxy.h:5: error: forward declaration of ‘struct IntList’ 
IntListProxy.cpp: In member function ‘IntListProxy*  `IntListProxy::append(IntListProxy*)’:` 
IntListProxy.cpp:43: error: invalid use of incomplete type ‘struct IntList’ 
IntListProxy.h:5: error: forward declaration of ‘struct IntList’ 
IntListProxy.cpp: In member function ‘int IntListProxy::operator[](int)’: 
IntListProxy.cpp:49: error: invalid use of incomplete type ‘struct IntList’ 
IntListProxy.h:5: error: forward declaration of ‘struct IntList’ 
IntListProxy.cpp:51: error: expected unqualified-id before ‘}’ token 
IntListProxy.cpp:51: error: expected ‘;’ before ‘}’ token 

IntListProxy.h

#include <iostream> 
#include <string> 

using namespace std; 
class IntList; 

class IntListProxy 
{ 
public: 
    IntListProxy(); 
    bool isEmpty(); 
    IntListProxy *prepend(int n); 
    int head(); 
    IntListProxy *tail(); 
    string toString(); 
    int length(); 
    IntListProxy *append(IntListProxy *lst); 
    int operator[](int n); 
private: 
    IntList *ptr; 

}; 

IntListProxy.cpp

#include "IntListProxy.h" 

IntListProxy::IntListProxy(){} 

bool IntListProxy::isEmpty(){ 

    ptr->isEmpty(); 

} 

IntListProxy *IntListProxy::prepend(int n){ 

    return ptr->prepend(n); 

} 

int IntListProxy::head(){ 

    return ptr->head(); 

} 

IntListProxy *IntListProxy::tail(){ 

    return ptr->tail(); 

} 

string IntListProxy::toString(){ 

    return ptr->toString(); 

} 

int IntListProxy::length(){ 

    return ptr->length(); 

} 

IntListProxy *IntListProxy::append(IntListProxy *lst){ 

    return ptr->append(lst); 

} 

int IntListProxy::operator[](int n){ 

    return ptr->operator[](n); 

} 

预先感谢您!

回答

2

建议的解决方案:
您需要包括它定义在你的CPP文件IntListProxy.cppIntList类的头文件。

说明:
,而不是包含头文件,当您添加一行:

class IntList; 

其转发声明类IntList这意味着编译器它是一个不完全类型。对于不完整的类型,不能创建它的对象或做任何需要编译器知道IntList的布局或者比IntList只是一个类型更多的事情。即:编译器不知道它的成员是什么,它的内存布局是什么。 但由于指向所有对象的指针只需要相同的内存分配,因此只需将指向Incomplete类型的指针时可以使用前向声明。

在这种情况下,您的cpp文件IntListProxy.cpp需要知道IntList的布局(成员),以便能够对其进行取消引用并因此引发错误。

0

在第7行,您致电isEmpty,IntList只有前向声明。编译器还没有看到该类的实际定义。

0

您刚刚宣布IntList。编译器不知道此struct中存在哪些方法。因此,您需要在代理cpp文件中包含包含IntList定义的头文件。