# Namespace problems [SOLVED]

This topic is 4839 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Here's the setup: clsA.h:
#ifndef CLSA_H
#define CLSA_H
namespace Ex
{

class A
{
...
};

} //namespace

#endif


clsB.h:
#ifndef CLSB_H
#define CLSB_H

#include "clsA.h"
#include <string>

namespace Ex
{

class B
{
public:
B( std::string& name, A* aClass );
...
protected:
Ex::A* mAClass;
std::string& mName;
};

} //namespace

#endif


The problem is this: The compiler is throwing errors both times class A is referenced in class B. In the constructor, it says that 'A' is unrecognized, and for the member object, it says that 'A' is not a member of namespace Ex. A wierd thing, though, is that IntelliSense knows it's in the namespace. Why would it be doing this? It's totally throwing me. Thanks for any help. [EDIT] Also, in B's constructor, the compiler gives an "unexpected type" error about std::string, but it gives nothing about the actual member string. [Edited by - ricekrispyw on August 14, 2005 11:11:08 PM]

##### Share on other sites
Shouldn't clsB.h include clsA.h ?

ace

##### Share on other sites
Sorry, the error was in an unrelated section of code, but the compiler was yelling about this part. Thanks a bunch.

[EDIT]
False alarm. I thought I'd found the problem. Yeah, clsA.h is included in clsB.h. I forgot to write it above. It's fixed now.

Another thing that might throw some light on the real problem: see the <string> problem in the modified original post.

[Edited by - ricekrispyw on August 14, 2005 10:19:19 PM]

##### Share on other sites
Quote:
 Original post by ricekrispywThe problem is this:The compiler is throwing errors both times class A is referenced in class B. In the constructor, it says that 'A' is unrecognized, and for the member object, it says that 'A' is not a member of namespace Ex.Why would it be doing this? It's totally throwing me.Thanks for any help.[EDIT]Also, in B's constructor, the compiler gives an "unexpected type" error about std::string, but it gives nothing about the actual member string.

One of the files you are including in class A's file, or in a file included by class A's file, is including class B's file. Since the inclussion happens at the top of class A's file, prior to class A's definition and you are apparently including class A's file prior to class B's file in code, class B's definition is encountered prior to class A's definition (if you don't understand this, you can confirm it by checking your compiler's preprocessor output, if it has such an option).

By using my magical powers, I predict that if you create one single cpp file that only includes class B's file, the errors you describe will go away in reference to that particular cpp file (other cpp files will still have the problem if they, at some point, include class A prior to class B).

To fix this problem, eliminate the unecessary inclussion of class B from class A's file or one of the files it includes if in fact the inclussion is unecessary (it's possible that it is necessary), and as well, only forward declare class A from class B's file rather than including it, if possible, since what you show of your current definition of class B only requires a declaration as opposed to a full definition. In case you are unfamiliar with forward declaring classes, the syntax is just simply:

class a;

##### Share on other sites
@Polymorphic OOP:
I think you very well may be right, and I think I have, in fact, encoutered something similar before. I should have though of that. I'll test it and get back to you.

[EDIT]
You ARE right! I had seen something like this before, but not with namespaces, and since I don't create stuff in namespaces much, I thought it must be something I didn't know about. Thanks a lot, man.

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 9
• 9
• 9
• 14
• 12
• ### Forum Statistics

• Total Topics
633297
• Total Posts
3011251
• ### Who's Online (See full list)

There are no registered users currently online

×