错误LNK2005和LNK1169

时间:2018-01-21 18:10:43

标签: c++

有很多问题,但没有一个能解决我的问题。我想使用多个文件。我不知道问题出在哪里,所以我将包括所有的顶线(#include,#pragma等)

当我为Cubes.cpp(以及Cubes.h)创建类时出现了这个问题。

的main.cpp

#include <iostream>
#include "Cubes.h"
#include "Common functions.h"
#include "Global.h"
#include <SFML/Graphics.hpp>

Cubes.cpp

#include <iostream>
#include "Cubes.h"
#include "Common functions.h"
#include "Global.h"
#include <SFML\Graphics.hpp>
#include <vector>
#include <string>

// Later on in code, where I think it went wrong:

Render::Render()
{
    this->rot;
    this->boxCoords;
    this->rlBoxCol;
    this->gridSize;
    this->cubeSize;
    this->offset;
}


void Render::setRotation(std::vector<float> setRot)
{ // Set rotation
    rot = setRot;
}
std::vector<float> Render::getRotation()
{ // Get rotation
    return rot;
}

void Render::setCoordinates(std::vector<int> setBoxCoords)
{
    boxCoords = setBoxCoords;
}
std::vector<int> Render::getCoordinates()
{
    return boxCoords;
}

void Render::setColours(std::vector<std::string> setRlBoxCol)
{
    rlBoxCol = setRlBoxCol;
}
std::vector<std::string> Render::getColours()
{
    return rlBoxCol;
}

void Render::setSizeOfGrid(int setGridSize)
{
    gridSize = setGridSize;
}
int Render::getSizeOfGrid()
{
    return gridSize;
}

void Render::setSizeOfCubes(int setCubeSize)
{
    cubeSize = setCubeSize;
}
int Render::getSizeOfCubes()
{
    return cubeSize;
}

void Render::setOffset(std::vector<int> setOffset)
{
    offset = setOffset;
}
std::vector<int> Render::getOffset()
{
    return offset;
}


void Render::display()
{
    // Long code, not going to show it
}

Cubes.h

#pragma once
#include <vector>
#include <string>

// Also later on in Cubes.h ...

class Render
{
private:
    std::vector<float> rot;
    std::vector<int> boxCoords;
    std::vector<std::string> rlBoxCol;
    int gridSize;
    int cubeSize;
    std::vector<int> offset;

public:
    Render();


    void setRotation(std::vector<float> setRot);
    std::vector<float> getRotation();

    void setCoordinates(std::vector<int> setBoxCoords);
    std::vector<int> getCoordinates();

    void setColours(std::vector<std::string> setRlBoxCol);
    std::vector<std::string> getColours();

    void setSizeOfGrid(int setGridSize);
    int getSizeOfGrid();

    void setSizeOfCubes(int setCubeSize);
    int getSizeOfCubes();

    void setOffset(std::vector<int> setOffset);
    std::vector<int> getOffset();


    void display();
};

常见的functions.cpp

#include <iostream>
#include "Common functions.h"
#include <vector>
#include <string>

Common functions.h

#pragma once
#include <vector>

Global.h

#pragma once
#include <SFML\Graphics.hpp>

extern sf::RenderWindow Window(sf::VideoMode(500, 500), "Maximize window to play the game");
extern std::string status = "NULL";

错误:

  

LNK2005

     

“class sf :: RenderWindow Window”(?Window @@ 3VRenderWindow @sf @@ A)已在Cubes.obj中定义

     

C:\ Users \ George \ Documents \ C ++ \ Projects \不要掉落\请勿掉落\ main.obj 1

  

LNK2005

     

“class std :: basic_string,class std :: allocator&gt; status”(?status @@ 3V?$ basic_string @ DU?$ char_traits @ D @ std @@ V?$ allocator @ D @ 2 @@ std @@ A)已在Cubes.obj中定义

     

C:\ Users \ George \ Documents \ C ++ \ Projects \不要掉落\请勿掉落\ main.obj 1

  

LNK1169

     

找到一个或多个多重定义的符号

     

C:\ Users \ George \ Documents \ C ++ \ Projects \不要掉落\ Debug \ Do not fall.exe 1

我一直坚持这个问题,所以任何帮助都会非常感激!

修改

我回答了我自己的问题,因为没有人找到解决方案。

1 个答案:

答案 0 :(得分:0)

解决方案(解决了我自己的问题)

Global.h中,我将代码更改为:

#pragma once
#include <SFML\Graphics.hpp>


extern sf::RenderWindow Window;
extern std::string status;

并在Cubes.cpp中,我将其更改为:

#include <iostream>
#include "Cubes.h"
#include "Common functions.h"
#include "Global.h"
#include <SFML\Graphics.hpp>
#include <vector>
#include <string>

sf::RenderWindow Window(sf::VideoMode(500, 500), "Maximize window to play the game");
std::string status = "NULL";
相关问题