Compileing errors dont know how to fix them, need help!

C++ Compileing error issues that i dont know how to fix my self

Page 1 of 1

2 Replies - 728 Views - Last Post: 14 September 2008 - 08:53 PM Rate Topic: -----

#1 Blake3334  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 14-September 08

Compileing errors dont know how to fix them, need help!

Post icon  Posted 14 September 2008 - 08:16 PM

The compileing errors i get when i compile the code, here is the compile error log:

----------------------------------------------------------------------------------------------

Compiler: Default compiler
Building Makefile: "C:\Documents and Settings\Compaq_Administrator\Desktop\BlakeScape\Blakescape ver 1.0\Makefile.win"
Executing make...
make.exe -f "C:\Documents and Settings\Compaq_Administrator\Desktop\BlakeScape\Blakescape ver 1.0\Makefile.win" all
g++.exe -D__DEBUG__ -c BlakeEngine.cpp -o BlakeEngine.o -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include" -I"C:/Dev-Cpp/include/c++/3.4.2/backward" -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32" -I"C:/Dev-Cpp/include/c++/3.4.2" -I"C:/Dev-Cpp/include" -pg -g3

In file included from BlakeEngine.cpp:4:
BlakeEngine.h: In function `int Engine_init_setup(int&, int&, int&)':
BlakeEngine.h:50: error: `screen' undeclared (first use this function)
BlakeEngine.h:50: error: (Each undeclared identifier is reported only once for each function it appears in.)
BlakeEngine.h: In function `bool Engine_init()':
BlakeEngine.h:60: error: `screen' undeclared (first use this function)
BlakeEngine.cpp: At global scope:
BlakeEngine.cpp:8: error: `SDL_Surface*screen' used prior to declaration

BlakeEngine.cpp:64: error: expected constructor, destructor, or type conversion before '=' token
BlakeEngine.cpp:64: error: expected `,' or `;' before '=' token
BlakeEngine.cpp:65: error: expected constructor, destructor, or type conversion before '.' token
BlakeEngine.cpp:65: error: expected `,' or `;' before '.' token
BlakeEngine.cpp:66: error: expected constructor, destructor, or type conversion before '.' token
BlakeEngine.cpp:66: error: expected `,' or `;' before '.' token
BlakeEngine.cpp:67: error: expected constructor, destructor, or type conversion before '.' token
BlakeEngine.cpp:67: error: expected `,' or `;' before '.' token

make.exe: *** [BlakeEngine.o] Error 1

Execution terminated

----------------------------------------------------------------------------------------------

What i am trying to do is create a engine using OOP and classes its basically my first time in c++ doing OOP and classes, but not my first time ever in other languages. I think the main problem is that i am doing something wrong in my OOP and i dont know why. I mainly do OOP in php and its easy has hell but in c++ i am kind of lost.





the first file is BlakeEngine.cpp





#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include <string>
#include "BlakeEngine.h"

//The surfaces
SDL_Surface *image = NULL;
SDL_Surface *screen = NULL;

//The event structure that will be used
SDL_Event event;

SDL_Surface *load_image( std::string filename ) 
{
	//The image that's loaded
	SDL_Surface* loadedImage = NULL;
	
	//The optimized image that will be used
	SDL_Surface* optimizedImage = NULL;
	
	//Load the image
	loadedImage = IMG_Load( filename.c_str() );
	
	//If the image loaded
	if( loadedImage != NULL )
	{
		//Create an optimized image
		optimizedImage = SDL_DisplayFormat( loadedImage );
		
		//Free the old image
		SDL_FreeSurface( loadedImage );
	}
	
	//Return the optimized image
	return optimizedImage;
};

void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination )
{
	//Temporary rectangle to hold the offsets
	SDL_Rect offset;
	
	//Get the offsets
	offset.x = x;
	offset.y = y;
	
	//Blit the surface
	SDL_BlitSurface( source, NULL, destination, &offset );
};














Blakescape = new Engine();
Blakescape.Engine();
Blakescape.Engine_init_setup( 1024, 768, 32 );
Blakescape.Engine_init();





bool load_files()
{
	//Load the image
	image = load_image( "x.png" );
	
	//If there was an error in loading the image
	if( image == NULL )
	{
		return false;	
	}
	
	//If everything loaded fine
	return true;	
};

void clean_up()
{
	//Free the surface
	SDL_FreeSurface( image );
	
	//Quit SDL
	SDL_Quit();	
};


int main( int argc, char* args[] )
{
	//Make sure the program waits for a quit
	bool quit = false;
	
	//Initialize
	if( Engine_init() == false )
	{
		return 1;	
	}
	
	//Load the files
	if( load_files() == false )
	{
		return 1;	
	}
	
	//Apply the surface to the screen
	apply_surface( 0, 0, image, screen );
	
	//Update the screen
	if( SDL_Flip( screen ) == -1 )
	{
		return 1;	
	}
	
	//While the user hasn't quit
	while( quit == false )
	{
		//While there's an event to handle
		while( SDL_PollEvent( &event ) )
		{
			//If the user has Xed out the window
			if( event.type == SDL_QUIT )
			{
				//Quit the program
				quit = true;
			}	
		}
	}
	
	//Free the surface and quit SDL
	clean_up();
		
	return 0;	
}




the second file which is the BlakeEngine.h or the header file.



/***************************************
*  Blakescape Engine 
*  Ver 1.0
^  Uses Simple DirectMedia Layer or (SDL)
*
****************************************/



/***************************************
*  Header files or files needed for the
*  Engine
*
****************************************/
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include <string>
using namespace std;

/***************************************
*  Global Variables
****************************************/
int m_iheight = 1024;
int m_iwidth = 768;
int m_ibpp = 32;







class Engine
{
	  public:
			 int Engine_init_setup();
			 bool Engine_init();
			 Engine();
								  
};


				

int Engine_init_setup( int & iwidth, int & iheight, int & ibpp)
{
	m_iwidth = iwidth;
	m_iheight = iheight;
	m_ibpp = ibpp;
	screen = SDL_SetVideoMode( iwidth, iheight, ibpp, SDL_SWSURFACE );
};
bool Engine_init()
{	
	 //Initialize all SDL subsystems
	if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
	{
		return false;	
	}	
	//If there was an error in setting up the screen
	if( screen == NULL )
	{
		return false;	
	}
	
	//Set the window caption
	SDL_WM_SetCaption( "Event test", NULL );
	
	//If everything initialized fine
	return true;
};



This post has been edited by Blake3334: 14 September 2008 - 08:20 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Compileing errors dont know how to fix them, need help!

#2 webboy42  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 17-August 08

Re: Compileing errors dont know how to fix them, need help!

Posted 14 September 2008 - 08:29 PM

Hi there
Are you new in C++? I ask because the errors you showed, that I read, are basic errors that are easy enough to fix. First, C++ is a case sensative language, make sure you use the right case for your identifiers (variables, methods, etc). Second, because of the nature of many C++ compilers, 1 typing error can cause a cascade effect, which seems to have happened in this case. Last, include the header that declares an identifier, and make sure you qualify the identifier if necessary.

I hope I've been able to help.

P.S. I just had a more substancial read of your post, and I see the problem, and also a problem you probably haven't noticed yet. First the problem I see. The "screen" variable is declared in the source file, not the header where the code that uses it is. Variable declarations in C++ MUST occur before the code that uses them, this applies to code in headers as well, since a header inclusion essentially inserts the code into the .cpp file. Second, you declared variables in the header file without the extern keyword, so in fact rather than being global, as you expected, they are local and the linker would likely complain of multiple declarations, because the declaration would appear in every source file that includes the header. For truely global data, declare variables as extern in a header, and make a normal declaration in 1 source file. If however the data is only needed by the class, but needs to be accessed by all instances, declare the variables as static data members.

This post has been edited by webboy42: 14 September 2008 - 08:47 PM

Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,516
  • Joined: 23-August 08

Re: Compileing errors dont know how to fix them, need help!

Posted 14 September 2008 - 08:53 PM

I'm going to have to say set your sights smaller to start. I realize like many newcomers you are eager to get right into writing games and other fun stuff, but you really need to have some of the basics down before you jump into the deep end. There are lots of tutorials on C++ and OOP here, as well as all over the Internet to help you out.

Anyway, some of the issues.

Engine_init and Engine_init_setup should be in the cpp file, not the header. They should be defined like this:
Engine::Engine()
{
    // your constructor code here
}

int Engine::Engine_init_setup(...)
{
    // Your code here
}

bool Engine::init()
{
    // Your code here
}



Generally, a header file will include declarations, with definitions and implementation done within the .cpp files.

This is all wrong:
Blakescape = new Engine();
Blakescape.Engine();
Blakescape.Engine_init_setup( 1024, 768, 32 );
Blakescape.Engine_init();


For one thing, it needs to be in the scope of the main routine. It should look something like:
int main()
{
    // Construct an Engine instance on the stack
    Engine blakeScape;

    // Init our instance's screen info
    blakeScape.init_setup(1024, 768, 32);

    // Finalize init (?)
    blakeScape.init();

    ...
}



Your "global variables" should also be member variables of the Engine class, and declared inside there.

class Engine
{
    public:
        int Engine_init_setup();
        bool Engine_init();
        Engine();
    
    private:
        int m_iheight;
        int m_iwidth;
        int m_ibpp;                                  
};

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1