我在用 C++ 编程时一直出现 LNK2005 错误 我似乎无法弄清楚是什么问题
I keep on having an LNK2005 error while programming in c++ I can't seem to figure out what is the issue
我搞不懂这两个错误是什么。
LNK2005 "public: bool __thiscall Bird::move(void)" (?move@Bird@@QAE_NXZ) 已经定义在 Bird.obj SFML-Game E:\Visual Studio Projects\SFML-Game\SFML-Game\main.obj
LNK1169 one or more multiply defined symbols found SFML-Game E:\Visual Studio Projects\SFML-Game\Debug\SFML-Game.exe
--------Bird.h------
#pragma once
#include <iostream>
#include <SFML/Window/Keyboard.hpp>
class Bird
{
public:
bool move();
private:
int gravity;
int velocity;
};
------Bird.cpp------
#pragma once
#include "Bird.h"
bool Bird::move() {
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) {
//TODO: Set Gravity to -1
std::cout << "..";
}
return true;
}
-----main.cpp-----
#include <SFML/Graphics.hpp>
#include <SFML/Window/Keyboard.hpp>
#include "Bird.cpp"
#include <random>
#include <iostream>
int main()
{
//Set the window to 800 by 600 pixels
sf::RenderWindow window(sf::VideoMode(600, 800), "Flappy Dot");
//Make a circle that is Blue
sf::CircleShape shape(20.f);
shape.setFillColor(sf::Color(255,255,255,100));
//While the window is open the constantly do these tasks
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear(sf::Color(255,255,255,100));
window.draw(shape);
window.display();
}
return 0;
}
这一行:
#include "Bird.cpp"
有效地将整个 Bird.cpp
复制粘贴到 main.cpp
中。假设 Bird.cpp
也是你的 VS 项目的一部分,这意味着它的内容将被编译和链接两次:一次作为文件本身,一次包含在 main.cpp
中的副本。这当然会导致像您看到的那样的多重定义错误。
几乎不需要 #include
一个 .cpp
文件在另一个文件中,当两个文件都正常编译时,绝对应该 不 完成联系在一起。只需将 main.cpp
中的那一行替换为:
#include "Bird.h"
我搞不懂这两个错误是什么。 LNK2005 "public: bool __thiscall Bird::move(void)" (?move@Bird@@QAE_NXZ) 已经定义在 Bird.obj SFML-Game E:\Visual Studio Projects\SFML-Game\SFML-Game\main.obj
LNK1169 one or more multiply defined symbols found SFML-Game E:\Visual Studio Projects\SFML-Game\Debug\SFML-Game.exe
--------Bird.h------
#pragma once
#include <iostream>
#include <SFML/Window/Keyboard.hpp>
class Bird
{
public:
bool move();
private:
int gravity;
int velocity;
};
------Bird.cpp------
#pragma once
#include "Bird.h"
bool Bird::move() {
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) {
//TODO: Set Gravity to -1
std::cout << "..";
}
return true;
}
-----main.cpp-----
#include <SFML/Graphics.hpp>
#include <SFML/Window/Keyboard.hpp>
#include "Bird.cpp"
#include <random>
#include <iostream>
int main()
{
//Set the window to 800 by 600 pixels
sf::RenderWindow window(sf::VideoMode(600, 800), "Flappy Dot");
//Make a circle that is Blue
sf::CircleShape shape(20.f);
shape.setFillColor(sf::Color(255,255,255,100));
//While the window is open the constantly do these tasks
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear(sf::Color(255,255,255,100));
window.draw(shape);
window.display();
}
return 0;
}
这一行:
#include "Bird.cpp"
有效地将整个 Bird.cpp
复制粘贴到 main.cpp
中。假设 Bird.cpp
也是你的 VS 项目的一部分,这意味着它的内容将被编译和链接两次:一次作为文件本身,一次包含在 main.cpp
中的副本。这当然会导致像您看到的那样的多重定义错误。
几乎不需要 #include
一个 .cpp
文件在另一个文件中,当两个文件都正常编译时,绝对应该 不 完成联系在一起。只需将 main.cpp
中的那一行替换为:
#include "Bird.h"