2011-10-31 77 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定義的頭文件。