Reputation: 65
Please help with this error. I am using a compiler called Code::Blocks. Here is my code.
#ifndef PROTOTYPES_H_INCLUDED
#define PROTOTYPES_H_INCLUDED
//The headers
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_ttf.h"
#include <string>
#include <fstream>
#include <sstream>
#include "Dot.h"
#include "vars.h"
using namespace std;
bool load_files( Dot &thisDot, Uint32 &bg );
#endif // PROTOTYPES_H_INCLUDED
When I try declare a prototype with the Dot parameter inside a header I get this annoying error. s "Dot was declared in this scope". Can anybody help with these errors they are really hindering my progression >.<
Here is the actual function which is inside a different a .cpp file inside the same directory (don't worry about linking, thats taken care of by the compiler):
bool load_files( Dot &thisDot, Uint32 &bg ) {
//Load the dot image
dot = load_image( "spritesheet.png" );
//Open font
font = TTF_OpenFont( "lazy.ttf", 28 );
//If there was a problem in loading the dot
if( dot == NULL )
{
return false;
}
if( font == NULL ) {
return false;
}
//Open a file for reading
std::ifstream load( "game_save" );
//If the file loaded
if( load != NULL )
{
//The offset
int offset;
//The level name
std::string level;
//Set the x offset
load >> offset;
thisDot.set_x( offset );
//Set the y offset
load >> offset;
thisDot.set_y( offset );
//If the x offset is invalid
if( ( thisDot.get_x() < 0 ) || ( thisDot.get_x() > SCREEN_WIDTH - DOT_WIDTH ) )
{
return false;
}
//If the y offset is invalid
if( ( thisDot.get_y() < 0 ) || ( thisDot.get_y() > SCREEN_HEIGHT - DOT_HEIGHT ) )
{
return false;
}
//Skip past the end of the line
load.ignore();
//Get the next line
getline( load, level );
load >> DOT_DIRECTION;
//If an error occurred while trying to read the data
if( load.fail() == true )
{
return false;
}
//If the level was white
if( level == "White Level" )
{
//Set the background color
bg = SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF );
}
//If the level was red
else if( level == "Red Level" )
{
//Set the background color
bg = SDL_MapRGB( screen->format, 0xFF, 0x00, 0x00 );
}
//If the level was green
else if( level == "Green Level" )
{
//Set the background color
bg = SDL_MapRGB( screen->format, 0x00, 0xFF, 0x00 );
}
//If the level was blue
else if( level == "Blue Level" )
{
//Set the background color
bg = SDL_MapRGB( screen->format, 0x00, 0x00, 0xFF );
}
//Close the file
load.close();
}
//If everything loaded fine
return true;
}
Here is 'Dot.h' which is in the same directory as all the other files:
#ifndef DOT_H_INCLUDED
#define DOT_H_INCLUDED
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_ttf.h"
#include <string>
#include <fstream>
#include <sstream>
#include "vars.h"
#include "prototypes.h"
//The dot
class Dot{
private:
//The X and Y offsets of the dot
int x, y;
//The velocity of the dot
int xVel, yVel;
//Game variables
int chakra, maxChakra;
public:
//Initializes the variables
Dot();
//Takes key presses and adjusts the dot's velocity
void handle_input();
//Moves the dot
void move();
//Shows the dot on the screen
void show();
//Set the dot's x/y offsets
void set_x( int X );
void set_y( int Y );
//Get the dot's x/y offsets
int get_x();
int get_y();
//Chakra
int getChakra();
int getMaxChakra();
void setChakra(int amount);
void useChakra(int amount);
void gainChakra(int amount);
};
Dot::Dot()
{
//Initialize the offsets
x = 0;
y = 0;
//Initialize the velocity
xVel = 0;
yVel = 0;
//Initialize chakra
maxChakra = 300;
chakra = maxChakra;
}
void Dot::set_x( int X )
{
x = X;
}
void Dot::set_y( int Y )
{
y = Y;
}
int Dot::get_x()
{
return x;
}
int Dot::get_y()
{
return y;
}
int Dot::getChakra(){
return chakra;
}
int Dot::getMaxChakra(){
return maxChakra;
}
void Dot::setChakra(int amount){
chakra = amount;
//if (chakra<0) bg = SDL_MapRGB( screen->format, 0xFF, 0x00, 0x00 );
}
void Dot::useChakra(int amount){
chakra -= amount;
//if (chakra<0) bg = SDL_MapRGB( screen->format, 0xFF, 0x00, 0x00 );
}
void Dot::gainChakra(int amount){
chakra += amount;
if (chakra>maxChakra) chakra = maxChakra;
}
void Dot::handle_input()
{
//If a key was pressed
if( event.type == SDL_KEYDOWN)
{
//Adjust the velocity
switch( event.key.keysym.sym )
{
case SDLK_UP:
yVel -= DOT_HEIGHT / 2;
if(!jutsuActive) DOT_DIRECTION = 0;
break;
case SDLK_DOWN:
yVel += DOT_HEIGHT / 2;
if(!jutsuActive) DOT_DIRECTION = 2;
break;
case SDLK_LEFT:
xVel -= DOT_WIDTH / 2;
if(!jutsuActive) DOT_DIRECTION = 3;
break;
case SDLK_RIGHT:
xVel += DOT_WIDTH / 2;
if(!jutsuActive) DOT_DIRECTION = 1;
break;
default: ;
}
}
//If a key was released
else if( event.type == SDL_KEYUP )
{
//Adjust the velocity
switch( event.key.keysym.sym )
{
case SDLK_UP: yVel += DOT_HEIGHT / 2; break;
case SDLK_DOWN: yVel -= DOT_HEIGHT / 2; break;
case SDLK_LEFT: xVel += DOT_WIDTH / 2; break;
case SDLK_RIGHT: xVel -= DOT_WIDTH / 2; break;
default: ;
}
}
}
void Dot::move()
{
//Move the dot left or right
x += xVel;
//If the dot went too far to the left or right
if( ( x < 0 ) || ( x + DOT_WIDTH > SCREEN_WIDTH ) || jutsuActive )
{
//Move back
x -= xVel;
}
//Move the dot up or down
y += yVel;
//If the dot went too far up or down
if( ( y < 0 ) || ( y + DOT_HEIGHT > SCREEN_HEIGHT ) || jutsuActive )
{
//Move back
y -= yVel;
}
}
void Dot::show()
{
//Show the dot
//apply_surface( x, y, dot, screen, &clipsChar[DOT_DIRECTION] );
}
#endif // DOT_H_INCLUDED
If you can help me fix this that would be amazing. Thanks.
Upvotes: 2
Views: 939
Reputation: 64308
You have a recursive include. prototypes.h is including Dot.h, and Dot.h is including prototypes.h.
Because the guards are preventing a true recursion, you just get an unexpected include order.
Because prototypes.h doesn't actually need the definition of class Dot, you could just forward declare Dot instead:
#ifndef PROTOTYPES_H_INCLUDED
#define PROTOTYPES_H_INCLUDED
//The headers
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_ttf.h"
#include <string>
#include <fstream>
#include <sstream>
#include "vars.h"
using namespace std;
class Dot;
bool load_files( Dot &thisDot, Uint32 &bg );
#endif // PROTOTYPES_H_INCLUDED
Upvotes: 3