代理的语法错误:我做错了什么? 目前我在我的书中学习C++,他们在使用以前创建的类叫做ItListar并使用ItListPosiver实现它的练习。我的书只在一个非常简单的例子中讨论代理,所以我很难理解它的语法。我用这个代理做了什么错事?我如何修复它?请记住,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代理的语法错误:我做错了什么? 目前我在我的书中学习C++,他们在使用以前创建的类叫做ItListar并使用ItListPosiver实现它的练习。我的书只在一个非常简单的例子中讨论代理,所以我很难理解它的语法。我用这个代理做了什么错事?我如何修复它?请记住,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,c++,proxy,C++,Proxy,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 to
#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;
};
提前谢谢你 在第7行调用
isEmpty
,IntList
仅被前向声明。编译器尚未看到该类的实际定义。您刚刚转发声明了IntList
。编译器不知道此结构中存在哪些方法。因此,您需要在代理cpp文件中包含包含IntList
定义的头文件。建议的解决方案:
您需要在cpp文件IntListProxy.cpp
中包含定义类IntList
的头文件
说明:
如果不包括头文件,则添加以下行:
class IntList;
它向前声明类IntList
,这意味着对于编译器来说,它是一个不完整类型。对于不完整的类型,不能创建它的对象,也不能做任何需要编译器知道IntList
布局的事情,或者仅仅知道IntList
只是一个类型。i、 e:编译器不知道它的成员是什么,它的内存布局是什么。
但是,由于指向所有对象的指针只需要相同的内存分配,所以在将不完整类型作为指针引用时,可以使用forward声明
在这种情况下,您的cpp文件IntListProxy.cpp
需要知道IntList
的布局(成员),才能取消对它的引用,从而消除错误
class IntList;