-1

我无法弄清楚这两个错误是什么。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;
}
4

1 回答 1

2

这一行:

#include "Bird.cpp"

有效地将整个复制粘贴Bird.cppmain.cpp. 假设Bird.cpp它也是您的 VS 项目的一部分,这意味着它的内容将被编译和链接两次:一次作为文件本身,一次作为包含在main.cpp. 这当然会导致像您所看到的那样的多重定义错误。

anohter 中几乎不需要#include一个文件,当两个文件都正常编译并链接在一起时,绝对不应该这样做。只需将该行替换为:.cppmain.cpp

#include "Bird.h"
于 2018-01-28T19:01:31.767 回答