Currently I’m learning C++ in my book and they had an exercise on using a previously created class called IntList and implement it using IntListProxy. My book only talks about proxies in a very simple example so I’m having a hard time with the syntax of it. What am I doing wrong with this proxy and how can I fix it? Keep in mind IntList is already a .o and I’m not allowed to include IntList.cpp when I compile this.
The error:
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);
}
Thank you in advance!
Suggested Solution:
You need to include the header file which defines class
IntListin your cpp fileIntListProxy.cpp.Explanation:
When instead of including the header file you add the line:
It Forward declares the class
IntListwhich means for compiler it is an Incomplete type. With Incomplete types, One cannot create objects of it or do anything which needs the compiler to know the layout ofIntListor more than the fact thatIntListis just an type. i.e: The compiler does not know what are its members and what its memory layout is.But Since pointers to all objects need just the same memory allocation, You can use the forward declaration when just reffering to an Incomplete type as a pointer.
In this case your cpp file
IntListProxy.cppneeds to know the layout(members) ofIntListto be able to dereference it and hence the error.