Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » "does not name a type" error, general c++ question

Credits go to James Stanley and Milan Mimica for helping out!
This thread is locked; no one can reply to it. rss feed Print
"does not name a type" error, general c++ question
evilmax17
Member #7,606
August 2006

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
Member #7,275
May 2006
avatar

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

Milan Mimica
Member #3,877
September 2003
avatar

Put definition of class A before class B.

edit: arhg, matter of seconds

evilmax17
Member #7,606
August 2006

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
Member #3,877
September 2003
avatar

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};

Go to: