我在c ++编程时一直出现LNK2005错误我似乎无法弄清问题是什么

时间:2018-01-28 18:57:52

标签: c++ sfml lnk2005

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

1 个答案:

答案 0 :(得分:2)

这一行:

Bird.cpp

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

在anohter中几乎不需要.cpp一个main.cpp文件,当两个文件正常编译并链接在一起时,它绝对应该。只需将#include "Bird.h" 中的那一行替换为:

void MainWindow::trainRecognizer(){

    cv::Ptr<cv::face::FaceRecognizer> recognizer = 
cv::face::LBPHFaceRecognizer::create();
    std::vector<cv::String> filename;
    cv::glob("validDataSet\\*.jpg", filename, false);
    std::vector<cv::Mat> images;
    std::vector<int> IDs;

   /*for (size_t i = 0; i < filename.size(); i++){
    qDebug() << QString::fromStdString(filename[i]) << "\n";
    cv::Mat image = cv::imread(filename[i]);
    QString imageName = QString::fromStdString(filename[i]).split("\\").at(1);
    QString id = imageName.split('.').at(1);
    images.push_back(image);
    IDs.push_back(id.toInt());
    }
    recognizer->train(images,IDs);
    recognizer->save("Recognizer\\sylvain.yml");*/
}