Page 1 of 4

[SOLVED] OOP X not declared! :@

Posted: Sat May 07, 2011 5:40 am
by VoidElite
Hey guys I'm getting really p***** off right now with this project! I'm working on a platformer game engine's box collision system and well O.O.P has failed me. :(

Basically the move() function which handles collision checking and movement requires the objects screen and the objects it's checking for collision with. One is of type Base and called base2, but now G++ is telling me that Base is undeclared. But you see the object that is intended to be passed as the operand of move() is also of type Base and named base and that works fine which means that clearly Base exists else lower down the line in Main.h, the declaration of 'Base *base;' wouldn't have been successful. Anyway here's the source code for a few of the files. :)

Globals.h

Code: Select all

//setting bullshit
//setting bullshit
#ifndef GLOBALS_H
#define GLOBALS_H
#include <iostream>
#include <sstream>
#include <fstream>
#include <cmath>
#include <ctime>
#include "SDL\SDL.h"
#include "Windows.h"

using namespace std;

bool quit;
const int SCREEN_BPP=32;
const int SCREEN_WIDTH=800;
const int SCREEN_HEIGHT=480;
const int SPRITE_MAX=375;
const int SPRITE_WIDTH=32;
const int SPRITE_HEIGHT=32;
const int BASE_HEIGHT=50;
const string SPRITE_FOLDER="Sprites\\";
const string FOREGROUND_FOLDER="Foreground\\";
const string BACKGROUND_FOLDER="Background\\";
const string PLAYER_FOLDER=FOREGROUND_FOLDER+"Player\\";
const string AI_FOLDER=FOREGROUND_FOLDER+"AIs\\";
const string OBJECT_FOLDER=FOREGROUND_FOLDER+"Objects\\";
const string BASE_FOLDER=FOREGROUND_FOLDER+"Bases\\";
const string SKY_FOLDER=BACKGROUND_FOLDER+"Skys\\";
const string PROP_FOLDER=BACKGROUND_FOLDER+"Props\\";
const string BREAK="----------------------------------------------------------------------";

//bullshiz
bool checkCollision(SDL_Rect a,SDL_Rect b);

#include "Debugger.h"
#include "Sprite.cpp"
#include "Player.cpp"
#include "AI.cpp"
#include "Object.cpp"
#include "Base.cpp"
#include "Sky.cpp"
#include "Prop.cpp"
#include "Lib.cpp"
#include "Trigger.cpp"
#endif
Player.h

Code: Select all

#ifndef PLAYER_H
#define PLAYER_H
#include "Globals.h"

using namespace std;

class Player{
	private:
		Sprite *sprite;
		string file;
		int x;
		int y;
		int xDelta;
		int yDelta;
	public:
		Player();
		Player(string file,int x,int y,int w,int h);
		~Player();
		void draw(SDL_Surface *screen);
		void checkInput(SDL_Surface *screen,SDL_Event event2,const int SPRITE_WIDTH,const int SPRITE_HEIGHT);
		void move(SDL_Surface *screen,Base *base2);
		string getFile();
		void setFile(string file2);
		SDL_Rect getBox();
		int getX();
		void setX(int x);
		int getY();
		void setY(int y);
		int getW();
		void setW(int w);
		int getH();
		void setH(int h);
		int getXDelta();
		void setXDelta(int xDelta2);
		int getYDelta();
		void setYDelta(int yDelta2);
};
#endif
Player.cpp

Code: Select all

#ifndef PLAYER_CPP
#define PLAYER_CPP
#include "Player.h"

Player::Player(string file,int x,int y,int w,int h){
	setXDelta(0);
	setYDelta(0);
	sprite=new Sprite(PLAYER_FOLDER+file,x,y,w,h);
}
Player::~Player(){
	delete sprite;
	delete this;
}
void Player::draw(SDL_Surface *screen){
	sprite->draw(screen);
}
void Player::checkInput(SDL_Surface *screen,SDL_Event event2,const int SPRITE_WIDTH,const int SPRITE_HEIGHT){
	//check event
	if(SDL_PollEvent(&event2)){
	if(event2.type==SDL_KEYDOWN){
		switch(event2.key.keysym.sym){
			case SDLK_ESCAPE:
				quit=true;
			break;
			case SDLK_UP: 
				setYDelta(getYDelta()-1);
			break;
            case SDLK_DOWN: 
				setYDelta(getYDelta()+1);
			break;
            case SDLK_LEFT: 	
				setXDelta(getXDelta()-1);
			break;
            case SDLK_RIGHT: 
				setXDelta(getXDelta()+1);
			break;
			default:
				//do nowt
			break;
		}
	}else if(event2.type==SDL_KEYUP){
		switch(event2.key.keysym.sym){
			case SDLK_UP: 
				setYDelta(getYDelta()+1);
			break;
            case SDLK_DOWN: 
				setYDelta(getYDelta()-1);
			break;
            case SDLK_LEFT:
				setXDelta(getXDelta()+1);
			break;
            case SDLK_RIGHT:
				setXDelta(getXDelta()-1);
			break;
			default:
				//do nowt
			break;
		}
	}else if(event2.type==SDL_QUIT){
		quit=true;
	}
	}
}
void Player::move(SDL_Surface *screen,Base *base2){
    setX(getX()+getXDelta());
    if((getX()<0)||(getX()+SPRITE_WIDTH>SCREEN_WIDTH)||(checkCollision(getBox(),base2->getBox()))){
        setX(getX()-getXDelta());
    }
    setY(getY()+getYDelta());
    if((getY()<0)||(getY()+SPRITE_HEIGHT>SCREEN_HEIGHT-BASE_HEIGHT)){
        setY(getY()-getYDelta());
    }
	draw(screen);
}
string Player::getFile(){
	return sprite->getFile();
}
void Player::setFile(string file2){
	sprite->setFile(file2);
}
SDL_Rect Player::getBox(){
	return sprite->getBox();
}
int Player::getX(){
	return sprite->getX();
}
void Player::setX(int x){
	sprite->setX(x);
}
int Player::getY(){
	return sprite->getY();
}
void Player::setY(int y){
	sprite->setY(y);
}
int Player::getW(){
	return sprite->getW();
}
void Player::setW(int w){
	sprite->setW(w);
}
int Player::getH(){
	return sprite->getH();
}
void Player::setH(int h){
	sprite->setH(h);
}
int Player::getXDelta(){
	return xDelta;
}
void Player::setXDelta(int xDelta2){
	xDelta=xDelta2;
}
int Player::getYDelta(){
	return yDelta;
}
void Player::setYDelta(int yDelta2){
	yDelta=yDelta2;
}
#endif
Here are the G++ errors:
In file included from src\Player.cpp:3,
from src\Globals.h:38,
from src\Globals.cpp:3,
from src\Main.h:1,
from src\Main.cpp:1:
src\Player.h:21: error: `Base' has not been declared
src\Player.h:21: error: ISO C++ forbids declaration of `base2' with no type
In file included from src\Globals.h:38,
from src\Globals.cpp:3,
from src\Main.h:1,
from src\Main.cpp:1:
src\Player.cpp:64: error: `Base' has not been declared
src\Player.cpp:64: error: ISO C++ forbids declaration of `base2' with no type
src\Player.cpp: In member function `void Player::move(SDL_Surface*, int*)':
src\Player.cpp:66: error: request for member `getBox' in `*base2', which is of n
on-class type `int'
src\Main.cpp: In function `void foregroundLayer()':
src\Main.cpp:37: error: no matching function for call to `Player::move(SDL_Surfa
ce*&, Base*&)'
src\Player.cpp:64: note: candidates are: void Player::move(SDL_Surface*, int*)
Help appreciated as always. :)

Re: OOP X not declared! :@

Posted: Sat May 07, 2011 7:24 am
by Milch
Okay, here's what I say:

1. Using a include-guard (the #ifdef part at the beginning of a file) inside a .cpp file is useless (because of point 2)
2. Do not include the .cpp files!

You should only include the .h/.hpp files, and not the .c/.cpp files!

Re: OOP X not declared! :@

Posted: Sat May 07, 2011 8:01 am
by cypher1554R
Theoretically, you want to add the .cpp files to the project, but not #include them in code like you do with .h.

Re: OOP X not declared! :@

Posted: Sat May 07, 2011 12:19 pm
by Falco Girgis
More like you failed OOP. ;)

Don't EVER. EVER. #include a CPP file. That's a terrible design practice. That's the same thing as the thread we had here just the other day talking about implementing everything inside of a header file and not using a CPP file...

You are literally recompiling your entire project every time you push build. That defeats the entire purpose of having a linker and using intermediate .o files. You might not care now, but as soon as your project gets any bigger, it's going to be a nightmare.

People have recommended you "add your CPP files to your project." I notice that you're using g++, which makes these recommendations slightly invalid. I remember back in the day, when I was first introduced to GCC/Linux development as well... I'm guessing you're doing this to avoid having to fuck with a makefile? You really need to throw together a very simple makefile invoking g++ for each .cpp file, so that you can simply include the header files. Post here if you need makefile help.

Re: OOP X not declared! :@

Posted: Sat May 07, 2011 12:59 pm
by VoidElite
GyroVorbis wrote:More like you failed OOP. ;)

Don't EVER. EVER. #include a CPP file. That's a terrible design practice. That's the same thing as the thread we had here just the other day talking about implementing everything inside of a header file and not using a CPP file...

You are literally recompiling your entire project every time you push build. That defeats the entire purpose of having a linker and using intermediate .o files. You might not care now, but as soon as your project gets any bigger, it's going to be a nightmare.

People have recommended you "add your CPP files to your project." I notice that you're using g++, which makes these recommendations slightly invalid. I remember back in the day, when I was first introduced to GCC/Linux development as well... I'm guessing you're doing this to avoid having to fuck with a makefile? You really need to throw together a very simple makefile invoking g++ for each .cpp file, so that you can simply include the header files. Post here if you need makefile help.
Well I was using a build script named build.bat(code):

Code: Select all

@echo off
tools\MinGW\bin\g++.exe src\Main.h -o build\game.exe -lmingw32 -lSDLmain -lSDL
pause
As I don't know how to make a makefile but I could, if that's what your suggesting, compile each file as a .o and link them within the script, would that mean I would completely remove the preprocessor directives? And if so would I have to add any more 'special' code. An example of this task would be nice Falco. :)

Re: OOP X not declared! :@

Posted: Sat May 07, 2011 1:45 pm
by N64vSNES
VoidElite wrote:
GyroVorbis wrote:More like you failed OOP. ;)

Don't EVER. EVER. #include a CPP file. That's a terrible design practice. That's the same thing as the thread we had here just the other day talking about implementing everything inside of a header file and not using a CPP file...

You are literally recompiling your entire project every time you push build. That defeats the entire purpose of having a linker and using intermediate .o files. You might not care now, but as soon as your project gets any bigger, it's going to be a nightmare.

People have recommended you "add your CPP files to your project." I notice that you're using g++, which makes these recommendations slightly invalid. I remember back in the day, when I was first introduced to GCC/Linux development as well... I'm guessing you're doing this to avoid having to fuck with a makefile? You really need to throw together a very simple makefile invoking g++ for each .cpp file, so that you can simply include the header files. Post here if you need makefile help.
Well I was using a build script named build.bat(code):

Code: Select all

@echo off
tools\MinGW\bin\g++.exe src\Main.h -o build\game.exe -lmingw32 -lSDLmain -lSDL
pause
As I don't know how to make a makefile but I could, if that's what your suggesting, compile each file as a .o and link them within the script, would that mean I would completely remove the preprocessor directives? And if so would I have to add any more 'special' code. An example of this task would be nice Falco. :)
Makefiles are daunting at first but you really need to look through some tutorials or maybe you have a book. It'll be a bitch to do but once you've gotten used to them you'll be glad that you looked into it.

As for preprocessor directives, from the makefile you can and specify what gets compiled and linked etc but you'll still need to guard your header files with #ifdef/#ifndef or #pragma once because you don't actually touch the headers from your makefile you'll just include them from your *.cpp files.

This tutorial seems fairly decent:
http://mrbook.org/tutorials/make/

Re: OOP X not declared! :@

Posted: Sat May 07, 2011 3:10 pm
by VoidElite
N64vSNES wrote:
VoidElite wrote:
GyroVorbis wrote:More like you failed OOP. ;)

Don't EVER. EVER. #include a CPP file. That's a terrible design practice. That's the same thing as the thread we had here just the other day talking about implementing everything inside of a header file and not using a CPP file...

You are literally recompiling your entire project every time you push build. That defeats the entire purpose of having a linker and using intermediate .o files. You might not care now, but as soon as your project gets any bigger, it's going to be a nightmare.

People have recommended you "add your CPP files to your project." I notice that you're using g++, which makes these recommendations slightly invalid. I remember back in the day, when I was first introduced to GCC/Linux development as well... I'm guessing you're doing this to avoid having to fuck with a makefile? You really need to throw together a very simple makefile invoking g++ for each .cpp file, so that you can simply include the header files. Post here if you need makefile help.
Well I was using a build script named build.bat(code):

Code: Select all

@echo off
tools\MinGW\bin\g++.exe src\Main.h -o build\game.exe -lmingw32 -lSDLmain -lSDL
pause
As I don't know how to make a makefile but I could, if that's what your suggesting, compile each file as a .o and link them within the script, would that mean I would completely remove the preprocessor directives? And if so would I have to add any more 'special' code. An example of this task would be nice Falco. :)
Makefiles are daunting at first but you really need to look through some tutorials or maybe you have a book. It'll be a bitch to do but once you've gotten used to them you'll be glad that you looked into it.

As for preprocessor directives, from the makefile you can and specify what gets compiled and linked etc but you'll still need to guard your header files with #ifdef/#ifndef or #pragma once because you don't actually touch the headers from your makefile you'll just include them from your *.cpp files.

This tutorial seems fairly decent:
http://mrbook.org/tutorials/make/
Ok now I'm really f****** confused. :\

I don't get it does my Main.cpp include the headers and the headers include the .cpps or does my Main.cpp include the .cpps and the .cpps include the headers ext.? :\

Re: OOP X not declared! :@

Posted: Sat May 07, 2011 3:25 pm
by dandymcgee
VoidElite wrote: Ok now I'm really f****** confused. :\

I don't get it does my Main.cpp include the headers and the headers include the .cpps or does my Main.cpp include the .cpps and the .cpps include the headers ext.? :\
Nothing ever includes .cpp files. Both the .cpps and main.cpp should each include the headers it needs.

Re: OOP X not declared! :@

Posted: Sat May 07, 2011 3:29 pm
by VoidElite
dandymcgee wrote:
VoidElite wrote: Ok now I'm really f****** confused. :\

I don't get it does my Main.cpp include the headers and the headers include the .cpps or does my Main.cpp include the .cpps and the .cpps include the headers ext.? :\
Nothing ever includes .cpp files. Both the .cpps and main.cpp should each include the headers it needs.
Yes but what if the X.cpp is used for the initialization of a class, X.h is for the declaration and Main.cpp wants to make an instance of the X class it will require access to X.cpp somehow or do I link the X.cpp with Main.cpp?

Re: OOP X not declared! :@

Posted: Sat May 07, 2011 4:01 pm
by Ginto8
for each source file, do this:

Code: Select all

g++ -o x.o -c x.cpp
This will compile each .cpp into a .o object file. Then, do

Code: Select all

g++ -o [executable name] a.o b.o ... x.o
This will link all the object files together into the executable. All interdependencies will be handled this way.

The advantage of this approach (which makefiles use) is that you only have to recompile the object files whose source files (and/or the headers those sources include) have changed. If they haven't, then just link them together again.

Re: OOP X not declared! :@

Posted: Sat May 07, 2011 4:27 pm
by N64vSNES
VoidElite wrote: Ok now I'm really f****** confused. :\

I don't get it does my Main.cpp include the headers and the headers include the .cpps or does my Main.cpp include the .cpps and the .cpps include the headers ext.? :\
Don't panic this crap can get very complex I know.

Basically you could do:

Code: Select all

g++ -o Main.cpp -o whatever1.cpp -o whatever2.cpp
etc

But as your project expands and you end up with like for example 80 source files, you really want to type this shit up? And then you also need linker dependencies and other crap on top of this.

A makefile is simply a reference for the compiler so it knows where to go, what to compile, what to link and how this should all get executed.

Just look through that tutorial piece by piece and you'll see it all come together.

Re: OOP X not declared! :@

Posted: Sat May 07, 2011 4:49 pm
by VoidElite
Ginto8 wrote:for each source file, do this:

Code: Select all

g++ -o x.o -c x.cpp
This will compile each .cpp into a .o object file. Then, do

Code: Select all

g++ -o [executable name] a.o b.o ... x.o
This will link all the object files together into the executable. All interdependencies will be handled this way.

The advantage of this approach (which makefiles use) is that you only have to recompile the object files whose source files (and/or the headers those sources include) have changed. If they haven't, then just link them together again.
I'm still trying to get my head around this stuff. So instead of porting the engine to this new method, I've decided to write a Main.cpp and a X.cpp just for testing. I've been compiling using this Batch script:

Code: Select all

@echo off
set g++=tools\MinGW\bin\g++.exe
%g++% -o bin\X.o -c X.cpp
%g++% -o bin\Main.o -c Main.cpp
%g++% -o game.exe bin\Main.o bin\X.o 
rem -lmingw32 -lSDLmain -lSDL
pause
Here's the code for Main.cpp:

Code: Select all

#include <iostream>

using namespace std;

int main(int argc,char *argv[]){
	Class *myClass=new Class();
	myClass->name="marc";
	cout<<"Hi"<<myClass->name<<endl;
	system("pause");
}
X.cpp:

Code: Select all

class Class{
	public:
		Class();
		char *name;
};
Class::Class(){
	name="Marc";
}
But you see I get this error:

Code: Select all

Main.cpp: In function `int main(int, char**)':
Main.cpp:6: error: `Class' was not declared in this scope
Main.cpp:6: error: `myClass' was not declared in this scope
Main.cpp:6: error: `Class' is not a type
So what modifications do I make to the build script to kinda preprocess X.cpp into Main.cpp without using preprocessor directives?

Re: OOP X not declared! :@

Posted: Sat May 07, 2011 5:05 pm
by N64vSNES
VoidElite wrote:
Ginto8 wrote:for each source file, do this:

Code: Select all

g++ -o x.o -c x.cpp
This will compile each .cpp into a .o object file. Then, do

Code: Select all

g++ -o [executable name] a.o b.o ... x.o
This will link all the object files together into the executable. All interdependencies will be handled this way.

The advantage of this approach (which makefiles use) is that you only have to recompile the object files whose source files (and/or the headers those sources include) have changed. If they haven't, then just link them together again.
I'm still trying to get my head around this stuff. So instead of porting the engine to this new method, I've decided to write a Main.cpp and a X.cpp just for testing.
You mean you're going to forget all about makefiles? Seriously you don't want to skip over that, if you're REALLY that bothered then get a IDE otherwise I very VERY strongly recommend you look into makefiles.
VoidElite wrote: So what modifications do I make to the build script to kinda preprocess X.cpp into Main.cpp without using preprocessor directives?
Why do you want to avoid using preprocessor directives so badly? :|

Re: OOP X not declared! :@

Posted: Sat May 07, 2011 5:27 pm
by VoidElite
N64vSNES wrote:
VoidElite wrote:
Ginto8 wrote:for each source file, do this:

Code: Select all

g++ -o x.o -c x.cpp
This will compile each .cpp into a .o object file. Then, do

Code: Select all

g++ -o [executable name] a.o b.o ... x.o
This will link all the object files together into the executable. All interdependencies will be handled this way.

The advantage of this approach (which makefiles use) is that you only have to recompile the object files whose source files (and/or the headers those sources include) have changed. If they haven't, then just link them together again.
I'm still trying to get my head around this stuff. So instead of porting the engine to this new method, I've decided to write a Main.cpp and a X.cpp just for testing.
You mean you're going to forget all about makefiles? Seriously you don't want to skip over that, if you're REALLY that bothered then get a IDE otherwise I very VERY strongly recommend you look into makefiles.
VoidElite wrote: So what modifications do I make to the build script to kinda preprocess X.cpp into Main.cpp without using preprocessor directives?
Why do you want to avoid using preprocessor directives so badly? :|
So I should use them? Ahh I sorta understand now. Hey how do I create and use variables in Makefiles?

Re: OOP X not declared! :@

Posted: Sat May 07, 2011 6:17 pm
by N64vSNES
Variables? Off the top of my head I believe they're declared like this:

Code: Select all

my_variable = something_for_example.cpp
And used like such:

Code: Select all

$(my_variable)