"does not name a type" error, general c++ question
evilmax17

Hello all,

I have this code:

class B{
public:
A hello;
B(){}
~B(){}
};

class A{
public:
A(){}
~A(){}
};

I get the error "A does not name a type" when declaring A hello.

Basically, I want to instatiate a member of class A in class B. What syntax am I missing?

Thanks in advance

James Stanley

You need to define A before B. Also, use code tags, it makes it easier to read.

Milan Mimica

Put definition of class A before class B.

edit: arhg, matter of seconds

evilmax17

Thanks for the response, but what if I have the following?

class B{
public:
A hello;
B(){}
~B(){}
};

class A{
public:
B hello;
A(){}
~A(){}
};

Milan Mimica

You cannot have that. Think about how much memory would that require - infinite. B includes A, which includes B, which includes A... But you can have pointers:

1 
2class A;
3 
4class B{
5public:
6A *hello;
7B(){}
8~B(){}
9};
10 
11class A{
12public:
13B *hello;
14A(){}
15~A(){}
16};

Thread #586909. Printed from Allegro.cc