simple OOP Linker error help

Started by
14 comments, last by sheep19 15 years, 11 months ago
Correct.

If a header does not even contain the name of another class - even if its corresponding .cpp file does - then it does not need any reference to it. The cpp file can take care of its own requirements.

If a header uses the name of another class and nothing further, you can usually get away with a "class OtherClass;" declaration.

If a header is using another class fully - e.g. you've got inline functions in your header that call the other class's methods - then you need a full #include "otherClass.h" directive.

If you can get away with no reference or a simple class declaration, you should - not just because it avoids circular dependencies, but because it reduces build times.

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

Advertisement
I've got this code now..

init.h
#ifndef INIT_H#define INIT_Hbool init(); // initialize SDL subsystems and TTFSDL_Surface* loadImage( std::string filename ); //the image loading function//apply a surface on the screenvoid applySurface( int x, int y, SDL_Surface* source, SDL_Surface* destination = SDL_GetVideoSurface() );#endif


init.cpp
#include "SDL/SDL.h"#include "SDL/SDL_image.h"#include "SDL/SDL_ttf.h"#include <string>#include "init.h"bool init(){	//initialize SDL subsystems	if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )	{		fprintf( stderr, "SDL could not be initialized.\n" );		return false;	}	else		atexit( SDL_Quit );	if( TTF_Init() == -1 )	{		fprintf( stderr, "TTF could not be initialized.\n" );		return false;	}	else		atexit( TTF_Quit );	return true;}SDL_Surface* loadImage( std::string filename ){	SDL_Surface* loadedImage = NULL;	SDL_Surface* optimizedImage = NULL;	loadedImage = IMG_Load( filename.c_str() );	if( loadedImage != NULL )	{		optimizedImage = SDL_DisplayFormat( loadedImage );		SDL_FreeSurface( loadedImage );	}	else		fprintf( stderr, "Image could not be loaded.\n" );	if( optimizedImage != NULL)     {         //map the color key, turquoise         Uint32 colorkey = SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF );                  SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, colorkey );     }	return optimizedImage;}void applySurface( int x, int y, SDL_Surface* source, SDL_Surface* destination ){	SDL_Rect offset;	offset.x = x;	offset.y = y;	SDL_BlitSurface( source, NULL, destination, &offset );}


main.cpp
#include "SDL/SDL.h"//#include <string> ERROR#include "init.h"const int SCREEN_WIDTH = 640;const int SCREEN_HEIGHT = 480;const int BPP = 32;int main( int argc, char* args[] ){	if( !init() )		return 0;	return 0;}


The problem is, if I don't #include <string> in main, I get a bunch of errors. My question is, why is this happening? I have included string in init.cpp and main doesn't use it..

Those are the errors I'm getting:
<code>
1>f:\my documents\visual studio 2005\projects\blocks\blocks\init.h(5) : error C2653: 'std' : is not a class or namespace name
1>f:\my documents\visual studio 2005\projects\blocks\blocks\init.h(5) : error C2065: 'string' : undeclared identifier
1>f:\my documents\visual studio 2005\projects\blocks\blocks\init.h(5) : error C2146: syntax error : missing ')' before identifier 'filename'
1>f:\my documents\visual studio 2005\projects\blocks\blocks\init.h(5) : error C2059: syntax error : ')'
</code>
Take a look at your init.h file. You use std::string. But where's <string>?
Quote:Original post by oler1s
Take a look at your init.h file. You use std::string. But where's <string>?


In init.cpp :-/

Quote:Original post by sheep19
and main doesn't use it..
Yes it does. It includes init.h, which uses it.

There is absolutely zero connection between a .h and .cpp file that both have the same name. None. It's purely a coding convention that you keep them in pairs. What init.cpp may or may not have in it has no effect on init.h.

Either you should put a forward declaration for std::string in the header - something like "class std::string;" - or you should put the include there instead of in the CPP files.

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

Thanks guys!

This topic is closed to new replies.

Advertisement