我的項目中有幾個文件無法按照我希望的方式工作。MSVC++ 2010快速鏈接器錯誤LNK2005
基本上,我有一個類,稱爲實體。它在Entity.h中定義,功能在Entity.cpp,所以這一切都是膨脹的。然後我有一個叫做PlayerShip的小孩課。它在兩個文件中以相同的方式定義。
(Entity.h在包含PlayerShip.h)
當我包括PlayerShip.h在playership.cpp並在main.cpp中接頭拋出一個LNK2005錯誤 - 構造函數被定義兩次。不過技術上來說,它不是隻是原型兩次?
的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));
}
我不知道爲什麼會發生這種情況。 = S
你還在''main.cpp'中包含'#include entity.h'嗎? – lapk
@user你能發佈確切的鏈接器錯誤信息嗎? – Mahesh
如果在main.cpp中同時包含「PlayerShip.h」和「Entity.h」,則必須使用[include guard](http://en.wikipedia.org/wiki/Include_guard)來保護頭文件。 –