problem with classes interaction
-
hey you guys, the problem is that i have 2 classes (say A,B) , class A have a pointer on class B
class A { B* b;};
and class B have a member variable for type class Aclass B {A a;};
this is seems to be a problem and the compiler retrun an error say 'a' uses undefined class 'A' although i am declaring the header of A#include "A.h" class B {A a;};
anybody have any idea abt wat might be the problem here thnx 4 ur time and concern learn, learn, and learn -
hey you guys, the problem is that i have 2 classes (say A,B) , class A have a pointer on class B
class A { B* b;};
and class B have a member variable for type class Aclass B {A a;};
this is seems to be a problem and the compiler retrun an error say 'a' uses undefined class 'A' although i am declaring the header of A#include "A.h" class B {A a;};
anybody have any idea abt wat might be the problem here thnx 4 ur time and concern learn, learn, and learnsingersinger wrote:
this is seems to be a problem and the compiler retrun an error say 'a' uses undefined class 'A' although i am declaring the header of A
Mr Compiler is right. You need to forward declare class B, which tells the compiler that there is a class called B, and he needn't worry about it.
class B;
class A
{ B* b;};class B
{A a;};
Nibu thomas A Developer Programming tips[^] My site[^]
-
hey you guys, the problem is that i have 2 classes (say A,B) , class A have a pointer on class B
class A { B* b;};
and class B have a member variable for type class Aclass B {A a;};
this is seems to be a problem and the compiler retrun an error say 'a' uses undefined class 'A' although i am declaring the header of A#include "A.h" class B {A a;};
anybody have any idea abt wat might be the problem here thnx 4 ur time and concern learn, learn, and learnYour problem is that a is not defined yet, as the compiler correctly says. The first thing it does when compiling A is go to B and compile that, and thus it does not know that class A exists yet. THe solution to this is to add the prototype to A before including B. e.g.
class A; #inlcude "B.h" class A { B* b;};
-
hey you guys, the problem is that i have 2 classes (say A,B) , class A have a pointer on class B
class A { B* b;};
and class B have a member variable for type class Aclass B {A a;};
this is seems to be a problem and the compiler retrun an error say 'a' uses undefined class 'A' although i am declaring the header of A#include "A.h" class B {A a;};
anybody have any idea abt wat might be the problem here thnx 4 ur time and concern learn, learn, and learnguys, thnx alot 4 ur fast reply but still this doesn't solve the problem, i don't know why :( --------------------- learn, learn, and learn
-
Your problem is that a is not defined yet, as the compiler correctly says. The first thing it does when compiling A is go to B and compile that, and thus it does not know that class A exists yet. THe solution to this is to add the prototype to A before including B. e.g.
class A; #inlcude "B.h" class A { B* b;};
wheelerbarry wrote:
class A;
#inlcude "B.h"
class A
{ B* b;};with this, you will have include problems. in B.h you will need to have include guards
// B.h
#ifndef _B_H_
#define _B_H_
class B
{
// ...
};
#endifand in A.h it is good practice ( IMO, YMMV ) to also add include guards :
// A.h
#ifndef _A_H_
#define _A_H_#ifndef _B_H_
#include "b.h"
#endifclass A
{
B* b;
};
#endifIMO, using forward declaration is the best approach to that problem unless
B
is not a pointer.
Maximilien Lincourt Your Head A Splode - Strong Bad
-
hey you guys, the problem is that i have 2 classes (say A,B) , class A have a pointer on class B
class A { B* b;};
and class B have a member variable for type class Aclass B {A a;};
this is seems to be a problem and the compiler retrun an error say 'a' uses undefined class 'A' although i am declaring the header of A#include "A.h" class B {A a;};
anybody have any idea abt wat might be the problem here thnx 4 ur time and concern learn, learn, and learn// A.h
#pragma onceclass B;
#include "B.h"
class A
{
B* b;
};// B.h
#pragma once#include "A.h"
class B
{
A a;
};// main.cpp
#include "B.h"void main( void )
{
B bb;
}
"Talent without discipline is like an octopus on roller skates. There's plenty of movement, but you never know if it's going to be forward, backwards, or sideways." - H. Jackson Brown, Jr.
"Judge not by the eye but by the heart." - Native American Proverb