forward declaration of classes and member functions

Jul 13, 2009 at 2:17am
Here is my problem:
I am trying to write two classes, one of which is a smart pointer to the other.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

class StudentPtr;

class Student
{
  private:
    //variables
  public:
    //constructor and other things
    StudentPtr &getPointer();
};

StudentPtr &Student::getPointer()
{
   StudentPtr *stu = new StudentPtr(this);
   return stu;
}

class StudentPtr
{
   private:
     Student *ptr;
   public:
     StudentPtr(Student *stu) :ptr(stu){}
};


When I try to compile this I get an error that says that the StudentPtr constructor has not been defined yet. Is there a way to do this without putting getPointer after the StudentPtr declaration?
Jul 13, 2009 at 2:35am
EDIT:

blah I'm blind. I didn't see StudentPtr declared there.

Is there a way to do this without putting getPointer after the StudentPtr declaration?


No. StudentPtr is an "include dependency" in getPointer, meaning the class must be fully defined by the time the function for that body exists.

http://cplusplus.com/forum/articles/10627/ <-- relevent linkage
Last edited on Jul 13, 2009 at 2:37am
Topic archived. No new replies allowed.