Class definition issue
-
Hello, I'm trying to recreate a problem that I'm having in another project and have created a small overly simple project to experiment with. That being said, I'm being blocked by what seems like a really silly problem. So, I have two classes: ClassA and ClassB. ClassB's constructor takes a pointer to ClassA as a parameter for later use. The compiler seems not to mind that, however it does not like when I try to use the pointer that is passed. Here's some code:
//ClassA.h
#pragma once;#include <iostream>
using namespace std;#include "ClassB.h"
class ClassB;class ClassA {
public:
ClassA () {
this->b = new ClassB(this);
}void printer() { cout << "Hello world from A!\\n"; }
private:
ClassB* b;};
And a little more:
//ClassB.h
#pragma once;#include <iostream>
using namespace std;#include "ClassA.h"
class ClassA;class ClassB {
public:
ClassB(ClassA *a) {
this->ca = a;
cout << "Constructor B \n\n";
ca->printer();
}private:
ClassA *ca;
};and finally a main to polish it off:
#include <windows.h>
#include <iostream>
using namespace std;#include "ClassA.h"
#include "ClassB.h"void main() {
ClassA a;
Sleep(10000);
}I get two errors with this setup. They both point to
ca->printer();
in the ClassB.h file. They areError 1 error C2027: use of undefined type 'ClassA' classb.h 14
andError 2 error C2227: left of '->printer' must point to class/struct/union/generic type classb.h 14
. Oddly enough, this isn't even the problem I'm trying to recreate from my larger project (I don't have this problem there). I'm actually trying to recreate an access violation on the call to the constructor of the class that ClassB represents (then the program continues, but it never executes the constructor). Anyway, I'll settle for figuring out what the silly mistake I'm making is that's blocking the compiler from finishing the job. Thanks for your help! -
Hello, I'm trying to recreate a problem that I'm having in another project and have created a small overly simple project to experiment with. That being said, I'm being blocked by what seems like a really silly problem. So, I have two classes: ClassA and ClassB. ClassB's constructor takes a pointer to ClassA as a parameter for later use. The compiler seems not to mind that, however it does not like when I try to use the pointer that is passed. Here's some code:
//ClassA.h
#pragma once;#include <iostream>
using namespace std;#include "ClassB.h"
class ClassB;class ClassA {
public:
ClassA () {
this->b = new ClassB(this);
}void printer() { cout << "Hello world from A!\\n"; }
private:
ClassB* b;};
And a little more:
//ClassB.h
#pragma once;#include <iostream>
using namespace std;#include "ClassA.h"
class ClassA;class ClassB {
public:
ClassB(ClassA *a) {
this->ca = a;
cout << "Constructor B \n\n";
ca->printer();
}private:
ClassA *ca;
};and finally a main to polish it off:
#include <windows.h>
#include <iostream>
using namespace std;#include "ClassA.h"
#include "ClassB.h"void main() {
ClassA a;
Sleep(10000);
}I get two errors with this setup. They both point to
ca->printer();
in the ClassB.h file. They areError 1 error C2027: use of undefined type 'ClassA' classb.h 14
andError 2 error C2227: left of '->printer' must point to class/struct/union/generic type classb.h 14
. Oddly enough, this isn't even the problem I'm trying to recreate from my larger project (I don't have this problem there). I'm actually trying to recreate an access violation on the call to the constructor of the class that ClassB represents (then the program continues, but it never executes the constructor). Anyway, I'll settle for figuring out what the silly mistake I'm making is that's blocking the compiler from finishing the job. Thanks for your help!You will need to move some code into a cpp file (or two): to be able to use a pointer for a class, the compiler needs to fully know the class. In your case, both classes are referencing each other and using the pointers in the header file. It's a snake that bites its own tail.
Cédric Moonen Software developer
Charting control [v1.4] OpenGL game tutorial in C++ -
You will need to move some code into a cpp file (or two): to be able to use a pointer for a class, the compiler needs to fully know the class. In your case, both classes are referencing each other and using the pointers in the header file. It's a snake that bites its own tail.
Cédric Moonen Software developer
Charting control [v1.4] OpenGL game tutorial in C++