Mark Berube
Mark Berube

Reputation: 204

Syntax Errors with Proxies: What am I doing wrong?

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!

Upvotes: 0

Views: 99

Answers (3)

Alok Save
Alok Save

Reputation: 206518

Suggested Solution:
You need to include the header file which defines class IntList in your cpp file IntListProxy.cpp.

Explanation:
When instead of including the header file you add the line:

class IntList;

It Forward declares the class IntList which 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 of IntList or more than the fact that IntList is 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.cpp needs to know the layout(members) of IntList to be able to dereference it and hence the error.

Upvotes: 2

Naveen
Naveen

Reputation: 73443

You have just forward declared IntList. Compiler doesn't know which methods exists in this struct. Hence you need to include the header file which contains the definition of IntList in the proxy cpp file.

Upvotes: 0

Dabbler
Dabbler

Reputation: 9863

At line 7, where you call isEmpty, IntList has only been forward-declared. The compiler has not yet seen the actual definition of the class.

Upvotes: 0

Related Questions