A class within a class
-
Hey. I'm trying to store a general, overall class that will store other classes within for a much easier accessible object, however, I keep running into the same problem. Here's a small example of the code i'm trying to produce:
class A {
public:
A();
~A();
};class B {
public:
B();
~B();
A Foo;
};And it keeps throwing the same error at me, that there's an unrecognised identifier before the variable 'Foo', and that default-int isn't supported (obviously). Class A is in a seperate header file, but i'm including it at the top of the header file for Class B. However, no matter what I try, it refuses to believe that 'class A' exists, and can't reference to it. I've even tried using the
typedef
keyword to declare 'class A' as an identifier, but even that won't work. I can't find any results over Google, nor here, about this problem. Can anyone assist? Thanks in advance. -
Hey. I'm trying to store a general, overall class that will store other classes within for a much easier accessible object, however, I keep running into the same problem. Here's a small example of the code i'm trying to produce:
class A {
public:
A();
~A();
};class B {
public:
B();
~B();
A Foo;
};And it keeps throwing the same error at me, that there's an unrecognised identifier before the variable 'Foo', and that default-int isn't supported (obviously). Class A is in a seperate header file, but i'm including it at the top of the header file for Class B. However, no matter what I try, it refuses to believe that 'class A' exists, and can't reference to it. I've even tried using the
typedef
keyword to declare 'class A' as an identifier, but even that won't work. I can't find any results over Google, nor here, about this problem. Can anyone assist? Thanks in advance.the following project
// A.h
#pragma onceclass A {
public:
A();
~A();
};// B.h
#pragma onceclass B {
public:
B();
~B();
A Foo;
};// A.cpp
#include "A.h"A::A(){}
A::~A(){}// B.cpp
#include "A.h"
#include "B.h"B::~B(){}
B::B(){}// APP.cpp
#include "A.h"
#include "B.h"void main()
{
A a;
B b;
}compiles fine even with
VC++ 6.0
. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Hey. I'm trying to store a general, overall class that will store other classes within for a much easier accessible object, however, I keep running into the same problem. Here's a small example of the code i'm trying to produce:
class A {
public:
A();
~A();
};class B {
public:
B();
~B();
A Foo;
};And it keeps throwing the same error at me, that there's an unrecognised identifier before the variable 'Foo', and that default-int isn't supported (obviously). Class A is in a seperate header file, but i'm including it at the top of the header file for Class B. However, no matter what I try, it refuses to believe that 'class A' exists, and can't reference to it. I've even tried using the
typedef
keyword to declare 'class A' as an identifier, but even that won't work. I can't find any results over Google, nor here, about this problem. Can anyone assist? Thanks in advance.This code (all in one file) compiles fine (with g++, but msvc will compile it as well) for me. There must be something else stopping B seeing A.
class A
{
public:
A() {}
~A() {}
};class B
{
public:
B() {}
~B() {}
A foo;
};One thing you could try is just running the code through the pre-processor (cl -E, if I remember rightly) to see what the compiler really sees.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
This code (all in one file) compiles fine (with g++, but msvc will compile it as well) for me. There must be something else stopping B seeing A.
class A
{
public:
A() {}
~A() {}
};class B
{
public:
B() {}
~B() {}
A foo;
};One thing you could try is just running the code through the pre-processor (cl -E, if I remember rightly) to see what the compiler really sees.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
Hm, running it through the pre-processor didn't really do much, since i'm pretty new to C++. All the headers are linked though. The header file which requires class 'A' has the inclusion for class A in the header. To be honest, I don't understand how it wouldn't be linking them together, they're all connected and 'class A' is definately being declared in (eg) 'A.h'. 'B.h' then includes 'A.h', and then requires 'A Foo' further down in the code. There are appropriate .cpp files that are also used with the initializers etc. and i've included both 'A.h' *and* 'B.h' in both of those files. It's just refusing to recognise 'class A' as any type of identifier, yet if I hover over the keyword 'A', it tells me that it's a class etc.
-
Hm, running it through the pre-processor didn't really do much, since i'm pretty new to C++. All the headers are linked though. The header file which requires class 'A' has the inclusion for class A in the header. To be honest, I don't understand how it wouldn't be linking them together, they're all connected and 'class A' is definately being declared in (eg) 'A.h'. 'B.h' then includes 'A.h', and then requires 'A Foo' further down in the code. There are appropriate .cpp files that are also used with the initializers etc. and i've included both 'A.h' *and* 'B.h' in both of those files. It's just refusing to recognise 'class A' as any type of identifier, yet if I hover over the keyword 'A', it tells me that it's a class etc.
Can you post the content of a.h, b.h?
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
Can you post the content of a.h, b.h?
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
Well, i've found a sort-of solution now. I'm currently declaring "class A;" after the "B.h" inclusion, but i'm going to run through my headers and make sure they're not getting mixed up. It might be that they're linked incorrectly (even if they're linked in the files that need them).
-
Well, i've found a sort-of solution now. I'm currently declaring "class A;" after the "B.h" inclusion, but i'm going to run through my headers and make sure they're not getting mixed up. It might be that they're linked incorrectly (even if they're linked in the files that need them).
Having a forward declaration for A (that's the 'class A;') shouldn't help in the definition of B - B would need to see the full definition of A to have a member of type A. Oh well. If you've got it to work...
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
Having a forward declaration for A (that's the 'class A;') shouldn't help in the definition of B - B would need to see the full definition of A to have a member of type A. Oh well. If you've got it to work...
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
My thoughts exactly. However, I placed #ifndef _SOCKETS_OBJ_DECLARED_ around the 'class A;', and in the 'B.h' at the end, I added #define _SOCKETS_OBJ_DECLARED_ Then, when I placed an #error "Undeclared" in the #ifndef, it threw the error.. so for some reason, the header is being included but it's not globally declaring the 'class B'. It's really strange, and another programmer said he couldn't explain it. The best I can do is go through it with a fine toothcomb and hopefully find where it's not declaring properly.
-
My thoughts exactly. However, I placed #ifndef _SOCKETS_OBJ_DECLARED_ around the 'class A;', and in the 'B.h' at the end, I added #define _SOCKETS_OBJ_DECLARED_ Then, when I placed an #error "Undeclared" in the #ifndef, it threw the error.. so for some reason, the header is being included but it's not globally declaring the 'class B'. It's really strange, and another programmer said he couldn't explain it. The best I can do is go through it with a fine toothcomb and hopefully find where it's not declaring properly.
I wonder if you've just got a missing semi-colon or something somewhere - that can cause really bizarre error messages and can be really hard to spot.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
I wonder if you've just got a missing semi-colon or something somewhere - that can cause really bizarre error messages and can be really hard to spot.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
I have no idea. I've been rooting through these header files, and as far as I can tell there are no missing semi-colons. In Visual C++, the actual region inside #ifndef _PACKETS_OBJ_DECLARED_ is completely grayed out, suggesting it should never be called, however it *is* being called, which might be something to do at compilation time. This has absolutely lost me, lol.