class referencing problem
-
hi, i have two classes in my program: class1 and class2. if i want to have them both include pointers to each other, how do I do that? one class must always be declared before the other. here is some example code:: main.cpp #include "class1.h" void main() { class1 test; } class1.h #pragma once #include "class2.h" class class1 { private: class2 *child1; class2 *child2; public: class1() { child1 = new class2(); child1->Init(); child2 = new class2(); child2->Init(); } }; class1.cpp #include "class1.h" class2.h #pragma once #include "class1.h" class class2 { private: class1 *parent; public: void Init(class1 *the_parent) { parent = the_parent; } }; class2.cpp #include "class2.h" Here is a snapshot of compiler output: Compiling... class2.cpp class1.h(5) : error C2143: syntax error : missing ';' before '*' class1.h(5) : error C2501: 'class1::class2' : missing storage-class or type specifiers class1.h(5) : error C2501: 'class1::child1' : missing storage-class or type specifiers class1.h(6) : error C2143: syntax error : missing ';' before '*' class1.h(6) : error C2501: 'class1::class2' : missing storage-class or type specifiers class1.h(6) : error C2501: 'class1::child2' : missing storage-class or type specifiers main.cpp class2.h(5) : error C2143: syntax error : missing ';' before '*' class2.h(5) : error C2501: 'class2::class1' : missing storage-class or type specifiers class2.h(5) : error C2501: 'class2::parent' : missing storage-class or type specifiers class2.h(7) : error C2061: syntax error : identifier 'class1' class1_1.cpp class2.h(5) : error C2143: syntax error : missing ';' before '*' class2.h(5) : error C2501: 'class2::class1' : missing storage-class or type specifiers class2.h(5) : error C2501: 'class2::parent' : missing storage-class or type specifiers class2.h(7) : error C2061: syntax error : identifier 'class1' one class's signature must be declared before the other, but they both reference each other.. how can this be done?
-
hi, i have two classes in my program: class1 and class2. if i want to have them both include pointers to each other, how do I do that? one class must always be declared before the other. here is some example code:: main.cpp #include "class1.h" void main() { class1 test; } class1.h #pragma once #include "class2.h" class class1 { private: class2 *child1; class2 *child2; public: class1() { child1 = new class2(); child1->Init(); child2 = new class2(); child2->Init(); } }; class1.cpp #include "class1.h" class2.h #pragma once #include "class1.h" class class2 { private: class1 *parent; public: void Init(class1 *the_parent) { parent = the_parent; } }; class2.cpp #include "class2.h" Here is a snapshot of compiler output: Compiling... class2.cpp class1.h(5) : error C2143: syntax error : missing ';' before '*' class1.h(5) : error C2501: 'class1::class2' : missing storage-class or type specifiers class1.h(5) : error C2501: 'class1::child1' : missing storage-class or type specifiers class1.h(6) : error C2143: syntax error : missing ';' before '*' class1.h(6) : error C2501: 'class1::class2' : missing storage-class or type specifiers class1.h(6) : error C2501: 'class1::child2' : missing storage-class or type specifiers main.cpp class2.h(5) : error C2143: syntax error : missing ';' before '*' class2.h(5) : error C2501: 'class2::class1' : missing storage-class or type specifiers class2.h(5) : error C2501: 'class2::parent' : missing storage-class or type specifiers class2.h(7) : error C2061: syntax error : identifier 'class1' class1_1.cpp class2.h(5) : error C2143: syntax error : missing ';' before '*' class2.h(5) : error C2501: 'class2::class1' : missing storage-class or type specifiers class2.h(5) : error C2501: 'class2::parent' : missing storage-class or type specifiers class2.h(7) : error C2061: syntax error : identifier 'class1' one class's signature must be declared before the other, but they both reference each other.. how can this be done?
class1.h
#pragma once
#include "class2.h"class class2; // <-- forward declaration. do the same thing in class2.h
class class1 {
private:
class2 *child1;
class2 *child2;
public:
class1()
{
child1 = new class2();
child1->Init();
child2 = new class2();
child2->Init();
}
}; -
hi, i have two classes in my program: class1 and class2. if i want to have them both include pointers to each other, how do I do that? one class must always be declared before the other. here is some example code:: main.cpp #include "class1.h" void main() { class1 test; } class1.h #pragma once #include "class2.h" class class1 { private: class2 *child1; class2 *child2; public: class1() { child1 = new class2(); child1->Init(); child2 = new class2(); child2->Init(); } }; class1.cpp #include "class1.h" class2.h #pragma once #include "class1.h" class class2 { private: class1 *parent; public: void Init(class1 *the_parent) { parent = the_parent; } }; class2.cpp #include "class2.h" Here is a snapshot of compiler output: Compiling... class2.cpp class1.h(5) : error C2143: syntax error : missing ';' before '*' class1.h(5) : error C2501: 'class1::class2' : missing storage-class or type specifiers class1.h(5) : error C2501: 'class1::child1' : missing storage-class or type specifiers class1.h(6) : error C2143: syntax error : missing ';' before '*' class1.h(6) : error C2501: 'class1::class2' : missing storage-class or type specifiers class1.h(6) : error C2501: 'class1::child2' : missing storage-class or type specifiers main.cpp class2.h(5) : error C2143: syntax error : missing ';' before '*' class2.h(5) : error C2501: 'class2::class1' : missing storage-class or type specifiers class2.h(5) : error C2501: 'class2::parent' : missing storage-class or type specifiers class2.h(7) : error C2061: syntax error : identifier 'class1' class1_1.cpp class2.h(5) : error C2143: syntax error : missing ';' before '*' class2.h(5) : error C2501: 'class2::class1' : missing storage-class or type specifiers class2.h(5) : error C2501: 'class2::parent' : missing storage-class or type specifiers class2.h(7) : error C2061: syntax error : identifier 'class1' one class's signature must be declared before the other, but they both reference each other.. how can this be done?
You need to use forward declarations. Try this:
// class1.h #pragma once class class2; class class1 { private: class2* child1; class2* child2; public: class1() { child1 = new class2(this); child2 = new class2(this); } }; // class2.h #pragma once class class1; class class2 { private: class1* parent; public: class2(class1* the_parent) : parent(the_parent) { } };
-- jlr http://jlamas.blogspot.com/[^] -
class1.h
#pragma once
#include "class2.h"class class2; // <-- forward declaration. do the same thing in class2.h
class class1 {
private:
class2 *child1;
class2 *child2;
public:
class1()
{
child1 = new class2();
child1->Init();
child2 = new class2();
child2->Init();
}
};thanks so much! first time i got an answer in a forum in only 2 minutes :-D
-
hi, i have two classes in my program: class1 and class2. if i want to have them both include pointers to each other, how do I do that? one class must always be declared before the other. here is some example code:: main.cpp #include "class1.h" void main() { class1 test; } class1.h #pragma once #include "class2.h" class class1 { private: class2 *child1; class2 *child2; public: class1() { child1 = new class2(); child1->Init(); child2 = new class2(); child2->Init(); } }; class1.cpp #include "class1.h" class2.h #pragma once #include "class1.h" class class2 { private: class1 *parent; public: void Init(class1 *the_parent) { parent = the_parent; } }; class2.cpp #include "class2.h" Here is a snapshot of compiler output: Compiling... class2.cpp class1.h(5) : error C2143: syntax error : missing ';' before '*' class1.h(5) : error C2501: 'class1::class2' : missing storage-class or type specifiers class1.h(5) : error C2501: 'class1::child1' : missing storage-class or type specifiers class1.h(6) : error C2143: syntax error : missing ';' before '*' class1.h(6) : error C2501: 'class1::class2' : missing storage-class or type specifiers class1.h(6) : error C2501: 'class1::child2' : missing storage-class or type specifiers main.cpp class2.h(5) : error C2143: syntax error : missing ';' before '*' class2.h(5) : error C2501: 'class2::class1' : missing storage-class or type specifiers class2.h(5) : error C2501: 'class2::parent' : missing storage-class or type specifiers class2.h(7) : error C2061: syntax error : identifier 'class1' class1_1.cpp class2.h(5) : error C2143: syntax error : missing ';' before '*' class2.h(5) : error C2501: 'class2::class1' : missing storage-class or type specifiers class2.h(5) : error C2501: 'class2::parent' : missing storage-class or type specifiers class2.h(7) : error C2061: syntax error : identifier 'class1' one class's signature must be declared before the other, but they both reference each other.. how can this be done?
that appeared to solve my problem, but now i have a new one. i get error C2512, no default constructor available, but there is a public constructor clearly defined with no parameters for both classes. this error occurs in the constructor for struct1. here is the code: main.cpp #include "class2.h" void main() { struct1 *config; config->item = new class2(); config->item->Init(config); } struct1.h #pragma once #include "class2.h" class class2; struct struct1 { private: class2 *item; public: struct1() { item = new class2(); /* ERROR OCCURS HERE while compiling main.cpp and class2.cpp */ } }; class1.h #pragma once class class1 { public: class1(){} }; class2.h #pragma once #include "class1.h" #include "struct1.h" struct struct1; class class2 : public class1 { private: struct1 *cfg; public: class2() {} void Init(struct1 *data) { cfg = data; } }; struct1.cpp #include "struct1.h" class1.cpp #include "class1.h" class2.cpp #include "class2.h"
-
that appeared to solve my problem, but now i have a new one. i get error C2512, no default constructor available, but there is a public constructor clearly defined with no parameters for both classes. this error occurs in the constructor for struct1. here is the code: main.cpp #include "class2.h" void main() { struct1 *config; config->item = new class2(); config->item->Init(config); } struct1.h #pragma once #include "class2.h" class class2; struct struct1 { private: class2 *item; public: struct1() { item = new class2(); /* ERROR OCCURS HERE while compiling main.cpp and class2.cpp */ } }; class1.h #pragma once class class1 { public: class1(){} }; class2.h #pragma once #include "class1.h" #include "struct1.h" struct struct1; class class2 : public class1 { private: struct1 *cfg; public: class2() {} void Init(struct1 *data) { cfg = data; } }; struct1.cpp #include "struct1.h" class1.cpp #include "class1.h" class2.cpp #include "class2.h"
The way you are mixing #includes and forward declarations is wrong. For example, take a look at what the compiler is seeing when you ask it to compile class2.cpp
- finds an include for "class2.h", so it goes for it.
- finds an include for "class1.h", so it goes for it.
- compiles the declaration for class1. Nothing else in this file, so it returns to class2.h
- finds an include for "struct1.h", so it goes for it.
- finds an include for "class2.h", but it's already started processing that file, and noted a "pragma once" directive, so it ignores this include.
- finds a forward declaration for class2, so it says to itself "I now know class2 is a class, even if I still don't know all the details"
- starts compiling the declaration for struct1. Finds the declaration of "item" as a pointer to a "class2". At this point, the compiler only needs to know wether the type of "item" is valid, and how much space it takes to store it. It does know the type is valid because of the forward declaration. And it does know how much space is needed for "item", because all pointers are the same size. Compilation goes on.
- It now reaches the line with "new class2()" in it, so it needs to create a call to some constructor of class2 with no parameters. The problem is it hasn't actually seen the complete declaration of class2 yet, so it doesn't know if such constructor exists
Reorganize your code as follows:
////////////////////////////////////////////////////////
// class1.h - Put class declaration here#pragma once
class class1
{
public:
class1();// Other members' declarations
};////////////////////////////////////////////////////////
// class1.cpp - Put class implementation here#include "class1.h"
class1::class1()
{
}// Other members' implementations
////////////////////////////////////////////////////////
// class2.h - Put class declaration here#pragma once
#include "class1.h"
struct struct1;
class class2 : public class1
{
private:
struct1* cfg;public:
class2();void Init(struct1* data);
};////////////////////////////////////////////////////////
// class2.cpp - Put class implementation here#include "class2.h"
class2::class2()
{
}void class2::Init(struct1* data)
{
// Consider moving this to the constructor and
// removing this function from the class,
// so that you don't need to always call Init()
// after the creation of each and ev -
The way you are mixing #includes and forward declarations is wrong. For example, take a look at what the compiler is seeing when you ask it to compile class2.cpp
- finds an include for "class2.h", so it goes for it.
- finds an include for "class1.h", so it goes for it.
- compiles the declaration for class1. Nothing else in this file, so it returns to class2.h
- finds an include for "struct1.h", so it goes for it.
- finds an include for "class2.h", but it's already started processing that file, and noted a "pragma once" directive, so it ignores this include.
- finds a forward declaration for class2, so it says to itself "I now know class2 is a class, even if I still don't know all the details"
- starts compiling the declaration for struct1. Finds the declaration of "item" as a pointer to a "class2". At this point, the compiler only needs to know wether the type of "item" is valid, and how much space it takes to store it. It does know the type is valid because of the forward declaration. And it does know how much space is needed for "item", because all pointers are the same size. Compilation goes on.
- It now reaches the line with "new class2()" in it, so it needs to create a call to some constructor of class2 with no parameters. The problem is it hasn't actually seen the complete declaration of class2 yet, so it doesn't know if such constructor exists
Reorganize your code as follows:
////////////////////////////////////////////////////////
// class1.h - Put class declaration here#pragma once
class class1
{
public:
class1();// Other members' declarations
};////////////////////////////////////////////////////////
// class1.cpp - Put class implementation here#include "class1.h"
class1::class1()
{
}// Other members' implementations
////////////////////////////////////////////////////////
// class2.h - Put class declaration here#pragma once
#include "class1.h"
struct struct1;
class class2 : public class1
{
private:
struct1* cfg;public:
class2();void Init(struct1* data);
};////////////////////////////////////////////////////////
// class2.cpp - Put class implementation here#include "class2.h"
class2::class2()
{
}void class2::Init(struct1* data)
{
// Consider moving this to the constructor and
// removing this function from the class,
// so that you don't need to always call Init()
// after the creation of each and evok thank you so much, my code compiles and runs now ;)