Reputation: 5
I have a few files in my project that aren't working the way I want them to.
Basically, I have a class, called Entity. It's defined in Entity.h and the functions are in Entity.cpp, so that's all swell. I then have a child class, called PlayerShip. It's defined the same way, in two files.
(Entity.h is included in PlayerShip.h)
When I include PlayerShip.h in playership.cpp AND in main.cpp the linker throws a LNK2005 error - the constructor is defined twice. Technically though, wasn't it just prototyped twice?
main.cpp
-- snip --
#include "PlayerShip.h"
using namespace std;
-- snip --
// PLAYER
int playerFlags = DRAW | EVENT | LOGIC;
playerShip pship = playerShip(playerFlags, iManager.getImage("ship.png"), 4);
Entity* player = eManager.addEntity(&pship);
etc, int main() yada yada yada
PlayerShip.h
#include "entity.h"
class playerShip : public Entity
{
private:
int horizontalSpeed, verticalSpeed;
int moveSpeed;
public:
playerShip(int allow, const sf::Image &img, int speed);
void handleLogic();
void handleEvents(sf::Event ev, sf::RenderWindow *screen);
};
playership.cpp
#include "PlayerShip.h"
playerShip::playerShip(int allow, const sf::Image &img, int speed) : Entity(allow, img), horizontalSpeed(0), verticalSpeed(0)
{
moveSpeed = speed;
}
void playerShip::handleEvents(sf::Event ev, sf::RenderWindow *screen)
{
while (screen->GetEvent(ev))
{
if (ev.Type == sf::Event::KeyPressed)
{
if (ev.Key.Code == sf::Key::Left)
horizontalSpeed = -1 * moveSpeed;
if (ev.Key.Code == sf::Key::Right)
horizontalSpeed = 1 * moveSpeed;
if (ev.Key.Code == sf::Key::Up)
verticalSpeed = -1 * moveSpeed;
if (ev.Key.Code == sf::Key::Down)
verticalSpeed = 1 * moveSpeed;
}
}
}
void playerShip::handleLogic()
{
setX(float(getX()+horizontalSpeed));
setY(float(getY()+verticalSpeed));
}
I have no clue why this is happening. =S
Upvotes: 0
Views: 314
Reputation: 102
I believe that you have forgotten include guards or using pragma once. These will make sure that the header will only be read once witch is good.
Include guard example:
#ifndef ENTITY_H
#define ENTITY_H
class Entity
...
#endif // ENTITY_H
Pragma once:
#pragma once
class Entity
...
Note that pragma once isn't standard but is still supported by practically all compilers.
Upvotes: 1