Recommended Posts

Using
C++, GCC, SDL 1.2.9, Code::Blocks 1.0 RC2, Windows XP
Problem
After adding the class XFont to the project, I receive an error: using-declaration for non-member at class scope on compile.
The odd part is that I have included the required header (map) in the accompanying CPP file. Prior to this particular class, the same process has worked fine with other classes. If I remove all inclusions of this class and drop it from the project, the project compiles fine.
Adding #include <map> to the header fixes it, but then it fails to compile other class headers using the same process.
Any suggestions?
XFont.h

Share this post

Link to post

Share on other sites

If you make mention of a type in a header file, then you need to have already declared the existence of that type. In your case, you make mention of the class std::map<...> in your header file, so the compiler has to have at least a little bit of a clue about this class. A forward declaration might be good enough, although from memory, I can only guess what a templated forward declaration would look like. But try putting this (or something similar) near the top of your header:

Normally, however, including <map> at the top of your header is perfectly fine, especially since <map> shouldn't be changing frequently, and thus shouldn't impact your compile times by being included in a header which gets included by multiple cpp files. But you mention that you have problems when adding <map> to the header. What are those problems, in more detail? Because this method should work, and is probably the preferred method for this situation.

Also, as for just including <map> in your cpp in the past, that's probably because you never needed to mention the std::map type in your header. As long as all use of the class is restricted to the cpp file, you only have to include the header somewhere in your cpp file. My guess is that this is probably the first time you had functions that pass std::map objects around.

Share this post

Link to post

Share on other sites

This compiles just fine, so long as XFont is not included in the project. When I include XFont, it complains as mentioned before. If I do include map in XFont.h, it begins complaining about the std::string and std::vector references in XTexture.h - prior to that, it doesn't.

Hence my curiousity to the point; if it worked before without complaint, why would a similar situation (std type inside a constructor prototype) complain, and only in that one instance?

Share on other sites

This compiles just fine, so long as XFont is not included in the project.

Do you mean if you don't have the .cpp included in the project? If that's the case, then the class XFont never needs to get compiled into an object file, and thus essentially gets ignored.

Quote:

When I include XFont, it complains as mentioned before. If I do include map in XFont.h, it begins complaining about the std::string and std::vector references in XTexture.h - prior to that, it doesn't.

Have you tried also including <string> and <vector> near the top of the XFont.h file as well? Because they should technically get included for the same reasons <map> should.

Quote:

Hence my curiousity to the point; if it worked before without complaint, why would a similar situation (std type inside a constructor prototype) complain, and only in that one instance?

My guess is that this instance was slightly different than previous instances in a variety of possible ways that aren't immediately apparent. For example, maybe the standard class files were included by some other include, but you didn't realize it.

This compiles and runs perfectly, so long as XFont.h and XFont.cpp aren't included in the project. When they are, but not referenced in main.cpp (no #include "TFont.h"), then XTexture.h begins complaining about std references.

I have since added all the includes to the header file and not the CPPs, and everything's peachy, I was moreso curious why it only borked after including XFont.