Click here to Skip to main content
15,355,296 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
First off, yes this is a university project, no I don't want you to write the program for me. This is a C++ project to write a game with the SDL libarary.

I have a problem with namespaces. for the project I need to separate my logical units from the part where I show them on the screen. I'm using the namespaces logic and visual to separate them. Now I need the Player class from the logic namespace to be the base class of the Player class in the visual namespace.

The visual::Player.h class looks like this:
#include "../logic/Player.h"

#ifndef PLAYER_H_
#define PLAYER_H_

namespace visual {

/**
 * This class represents a player in the game. This part of the class will
 * put the player score and remaining lives on the screen
 */
class Player : public logic::Player {
public:
	/**
	 * constructor for the player class
	 */
	Player();

	/**
	 * destructor for the player class
	 */
	virtual ~Player();


};

}
#endif /* PLAYER_H_ */


And the visual::Player.cpp file looks like this:
#include "Player.h"

namespace visual {

Player::Player() {
	// TODO Auto-generated constructor stub

}
Player::~Player() {
	// TODO Auto-generated destructor stub
}

}


Now I'm getting errors on the lines in the .cpp file:
Player::Player()
Multiple markers at this line
- ISO C++ forbids declaration of ‘Player’ with no
type
- ‘Player’ has not been declared

Player::~Player()
expected constructor, destructor, or type conversion before ‘::’ token
Posted

I broke your code down to its smallest component thus:
C++
namespace logic {
    class Player {
    public:
        Player() {}
        virtual ~Player(){}
    };
}
namespace visual {
    class Player : public logic::Player {
    public:
        Player();
        virtual ~Player();
    };
}
using namespace visual;
Player::Player() {
    // TODO Auto-generated constructor stub
}
Player::~Player() {
    // TODO Auto-generated destructor stub
}

The above compiles without error in Visual C++ 2010.
   
What you've written looks valid and I can get something comparable compiling on a whole bunch of different compilers. However my good taste sensor when "bleugh" when I saw the derivation from logic::Player. Without seeing the code in there it's hard to tell but does the problem go away if you remove the base class? If it does the problems somewhere a bit deeper than what you've shown us.

Cheers,

Ash
   
I think your namespace visual declaration in your .cpp file should be replaced by a using namespace visual; statement.
   
Comments
Emilio Garavaglia 27-Nov-10 13:04pm
   
Didn't Work. I changed my .cpp code to:
Collapse

#include "Player.h"

using namespace visual;

Player::Player() {
// TODO Auto-generated constructor stub

}
Player::~Player() {
// TODO Auto-generated destructor stub
}


I still get the same errors.

Emilio Garavaglia on behalf of KenBonny.
Paul Michalik 28-Nov-10 6:25am
   
No, never do this. This is ill-formed code, since the declaration and the definition of Player's methods are in different namespaces now. Player's declaration is in visual namespace and the definitions of it's methods are in global namespace. The 'using' directive does not open a namespace!
The logic::Player is the logic side of the side that computes positions and keeps track of score. This is for a pong like game, so there will be a logic::Ball and a visual::Ball. the logic::Ball will have to get me the positions of the ball and the visual::Ball will show the appropriate sprite or image on the screen according to the positions the logic::Ball computes. That's the reasoning behind this code, so basically it's to separate the logic side from the visual side.

Can you tell me why this is such a horrible design idea? I'm still learning here.

Just removed the reference to logic::Player from the visual::Player and I still get the errors. My files do have the same name, but they are in different maps.
   
using like this
<br />
using namespace visual;<br />
Player::Player() {    // TODO Auto-generated constructor stub}Player::~Player() {    // TODO Auto-generated destructor stub}<br />
//this is right<br />
   
I got it. I didn't know
#ifndef PLAYER_H_
#define PLAYER_H_

had to be different for the two files. So I changed them to PLAYER_Logic_H_ and PLAYER_Visual_H_, now the code works.

The fist one who posts this solution will get an "Accept Answer".
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900