Why is this undefined.

I have a project that I am working on and I am forced to use forward declaration, but member variables are said to be undeclaraed, Here is an example of what I am talking about.

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
using namespace std;
class B;
class A
{
public:
	A(B* PointB):
	  PointB(PointB)
	{
	}
	~A()
	{
	}
	void func(B* b)
	{
		b->func();
	}
	B* PointB;
};

class B
{
public:
	B()
	{
		PointA = new A(this);
			}
	~B()
	{
	}
	void func()
	{
		cout<< "func b \n";
		
	}
	A* PointA;
};

int main()
{	
	B* TestB;
	TestB = new B;
	return 0;
}

The problem is, b (thats lowercase b) from classs A is said to be undeclared, so it doesnt compile, could anyone give me reasons.
As you could see b is never inniallized untiled invoked.
Last edited on
Hmmm. Ok.

This is undefined because. You have forward declared that a class called "B" exists. However, that is all the information you have declared. So A is unable to call any functions on B because it doesn't know what B is exposing. This is one of the reasons why you use Header files (.h) to prevent crap like this :P

You could do:

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
using namespace std;
class B;
class A
{
public:
  A(B* PointB):
    PointB(PointB)
  {
  }
  ~A()
  {
  }
  void func(B* b);  // Declare
  B* PointB;
};

class B
{
public:
  B()
  {
    PointA = new A(this);
      }
  ~B()
  {
  }
  void func()
  {
    cout<< "func b \n";
    PointA->func(this);
  }
  A* PointA;
};

// Implement A->func now. After Class B's layout has been declared
void A::func(B* b) {
    b->func();
}

int main()
{ 
  B* TestB;
  TestB = new B;
  return 0;
}


HOWEVER. Your A::func calls B::func and your B::func calls A::func. Thats an infinite loop right there. So you won't be able to use that anyways.

As you can see from my resolution of your problem. This is exactly why class definitions are put into .H files and the implementation into .cpp
Last edited on
Thanks for the reply, and the answer, but this was really an example of my problem, my problem is really more deeper, I have multiple class (all declarations in .h files all definitions in .cpp), say one class is movements, one animal and another is dog which is derived from animal. here is how its presented. (example again, my codes are using 3rd party libraries so I cant use it)
mevement.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "dog.h"
#include <set>
 

class movement
{
public:
	movement();
	~movement();
	void func(animal* anim);
	void make();
private:
	animal* anim[10];
	int i;
        std::set<animal*>RegisterAnimal;
};


movement.cpp
1
2
3
4
5
6
7
8
9
10
11
void movement::func(animal* anim)
{
	RegisterAnimal.insert(anim);
}

void movement::make()
{
	//i = 0; decalred as 0 in another function
	anim[i] = new dog(this);
	i++
}


dog.h
1
2
3
4
5
6
7
8
9
10
11
12
#include "animal" 

class dog: public animal
{
public:
	dog(movement* move);
	~dog();
	/* use member functions and variables
	inherited from animal*/

private:
};


dog.cpp
1
2
3
4
dog::dog(movement *move):
animal(move)
{
}


animal.h

1
2
3
4
5
6
7
8
9
10
11
class movement;
class animal
{
public:
	animal(movement* move);
	~animal();
	/* house many data members*/
protected:
	movement* movepointer;
};


animal.cpp
1
2
3
4
5
6
animal::animal(movement *move):
movepointer(move)
{
	movepointer->func(this)
}


as you can see I cant include movement.h in animal (gonna cause recursion) so I used forward declaration but It wont compile because the movepointer member varaible in animal is said to be undefined.
anyone got any ideas how to solve this problem.
Last edited on
Try to avoid programming like this in the future...

However if you really want to stick with your guns, include movement.h in your animal.cpp.
Rofl I went to bed and had a dream about doing the same thing, now it works like a charm :D,
Yeah I gotta really avoid doing stuff like this though.I am gonna see how I could avoid this.
Topic archived. No new replies allowed.