Compiler complains about using-declaration

Started by
5 comments, last by tychon 18 years, 2 months ago
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

#ifndef XFONT_H
#define XFONT_H

namespace XGameLib
{
	class XFont
	{
		private:
			SDL_Surface* mSurface;
			std::map< char, SDL_Rect > mClip;

		public:
			XFont( std::string, std::map< char, SDL_Rect > );

			~XFont();

		public:
			XTexture* GetString( std::string );
	};
}

#endif

XFont.cpp

#include <sdl/sdl.h>

#include <map>
#include <string>

#include "XTexture.h"
#include "XFont.h"

namespace XGameLib
{
	XFont::XFont( std::string font, std::map< char, SDL_Rect > clip )
		: mClip( clip )
	{
		SDL_Surface* temp = SDL_LoadBMP( font.c_str() );

		if ( temp != NULL )
		{
			mSurface = SDL_DisplayFormat( temp );
			SDL_FreeSurface( temp );
		}
	}

	XFont::~XFont()
	{
		if ( mSurface != NULL )
			SDL_FreeSurface( mSurface );
	}

	XTexture* XFont::GetString( std::string message )
	{
		SDL_Surface* output;

		for ( int i = 0; i < message.length(); ++i )
		{

		}

		return new XTexture( output );
	}
}

Advertisement
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:
namespace std{  template<class Key, class T, class Pred = less<Key>, class A = allocator<T> > class map;}

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.
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
What line does the compiler point to when it gives the error message?
For example: XTexture( std::string, std::vector< SDL_Rect >, int );

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?
Quote:Original post by SiCrane
What line does the compiler point to when it gives the error message?


Oh, apologies. Lines 10 and 13. 18 isn't pointed to, despite having the std::string reference.
Quote:Original post by tychon
For example: XTexture( std::string, std::vector< SDL_Rect >, int );

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.

"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
XTexture.h
#ifndef XTEXTURE_H#define XTEXTURE_Hnamespace XGameLib{	class XTexture	{		private:			std::vector< SDL_Surface* > mSurface;			std::vector< SDL_Rect > mClip;			int mDelay;			int mCurrentFrame;			Uint32 mFrameStart;		private:			int LoadImage( std::string );		public:			XTexture( SDL_Surface* );			XTexture( std::string ); // single-image			XTexture( std::vector< std::string >, int ); // multi-image anim			XTexture( std::string, std::vector< SDL_Rect >, int ); // single-image anim			XTexture( std::string, std::string, int ); // single-image anim, file-clip			~XTexture();		public:			int Render( XScreen&, int, int );			void NextFrame();			void PreviousFrame();	};}#endif


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.

This topic is closed to new replies.

Advertisement