C++ 出现 LNK2019 错误,即使没有循环依赖,也没有包含两次

标签 c++ visual-studio visual-c++ entity

好的,所以我正在尝试创建一个实体组件系统,直到现在它工作得相当不错,但现在我遇到了一些链接器错误,我不确定为什么,我已经尝试了几个小时的不同变体但我无法让它工作,我知道我犯了一个非常愚蠢的错误,但我找不到它, 所以这里是我的所有文件:(一般什么都不做!,我还没有触及主要文件,我想链接器错误是由实体和公共(public)文件引起的)

基本上我有一个巨大的包装器,即“Public.h”,其中包含必要的 header ,并具有一些可重用的方法,这是 Public.h:

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

using namespace std;
static int counter = 0;
static int NamCounter = 0;
static int TagCounter = 0;
static vector <string> AllNames;
static vector <string> AllTags;
//static vector <Entity> AllEntities;

//static void AddToAllENT(Entity* newE){
//}

int GetCounter();
int GetNamCounter();
int GetTagCounter();
string UniqueName(string uN = "lol");
string UniqueTag(string uT = "lol");

一切正常,这是“Public.cpp”:

#include "Public.h"

//static void AddToAllENT(Entity* newE){
//}

static int GetCounter(){
    return counter++;
}
static int GetNamCounter(){
    return TagCounter++;
}
static int GetTagCounter(){
    return NamCounter++;
}
static string UniqueName(string uN){
    if (uN == "lol")
        return "_Entity" + to_string(GetNamCounter());
    for (int i = 0; i < AllTags.size(); i++){
        if (AllTags.at(i) == uN)
            return "_Entity" + to_string(GetNamCounter());
    }
    AllTags.push_back(uN);
    return uN;
}
static string UniqueTag(string uT){
    if (uT == "lol")
        return "_EntityTag" + to_string(GetTagCounter());
    for (int i = 0; i < AllTags.size(); i++){
        if (AllTags.at(i) == uT)
            return "_EntityTag" + to_string(GetTagCounter());
    }
    AllTags.push_back(uT);
    return uT;
}

那么我的问题出在实体文件上,这是我的 Entity.h:

#pragma once

class Entity
{
private:
    int ID;
    string Name;
    string Tag;
    //vector <Component> AllComps;
public:
    friend bool operator==(const Entity& left, const Entity& right){
        return ((left.Name == right.Name) && (left.Tag == right.Tag));
    }
    friend bool operator!=(const Entity& left, const Entity& right){
        return !(left == right);
    }
    Entity();
    Entity(const Entity&);
    Entity(string);
    Entity(string, string);
    ~Entity();
    int GetID();
    string GetName();
    string GetTag();
    void Start();
    void Update();
    void SetAll(string, string);
    void PrintAll();
};

这是我的 Entity.cpp:

#include "Public.h"
#include "Entity.h"

Entity::Entity()
{
    ID = GetCounter();
    this->Name = UniqueName();
    this->Tag = UniqueTag();
}
Entity::Entity(string name){
    ID = GetCounter();
    this->Name = UniqueName(name);
    this->Tag = UniqueTag();
}
Entity::Entity(string name, string tag){
    ID = GetCounter();
    this->Name = UniqueName(name);
    this->Tag = UniqueTag(tag);
}

int Entity::GetID(){ return this->ID; }
string Entity::GetName(){ return this->Name; }
string Entity::GetTag(){ return this->Tag; }
void Entity::PrintAll(){
    cout << "NAME: \"" << this->Name << "\" TAG: \"" << this->Tag << "\" ID: \"" << this->ID << "\"" << endl;
}
void Entity::SetAll(string newn, string newt){
    this->Tag = newt;
    this->Name = newn;
}

void Entity::Start(){

}
void Entity::Update(){

}

Entity::~Entity()
{
}

所有这一切导致我出现很多链接器错误,我不确定为什么,我很确定所有内容都正确链接,并且没有包含两次等,但我仍然遇到这些错误:

Error   1   error LNK2019: unresolved external symbol "int __cdecl GetCounter(void)" (?GetCounter@@YAHXZ) referenced in function "public: __thiscall Entity::Entity(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (??0Entity@@QAE@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@0@Z)    C:\Users\drin-_000\Documents\Visual Studio 2013\Projects\ECSystem\ECSystem\Entity.obj   ECSystem
Error   2   error LNK2019: unresolved external symbol "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl UniqueName(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?UniqueName@@YA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V12@@Z) referenced in function "public: __thiscall Entity::Entity(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (??0Entity@@QAE@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@0@Z)  C:\Users\drin-_000\Documents\Visual Studio 2013\Projects\ECSystem\ECSystem\Entity.obj   ECSystem
Error   3   error LNK2019: unresolved external symbol "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl UniqueTag(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?UniqueTag@@YA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V12@@Z) referenced in function "public: __thiscall Entity::Entity(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (??0Entity@@QAE@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@0@Z)    C:\Users\drin-_000\Documents\Visual Studio 2013\Projects\ECSystem\ECSystem\Entity.obj   ECSystem
Error   4   error LNK1120: 3 unresolved externals   C:\Users\drin-_000\Documents\Visual Studio 2013\Projects\ECSystem\Debug\ECSystem.exe    ECSystem

最佳答案

删除 static从实现那些GetCounter , GetNamCounter , GetTagCounter , UniqueName , UniqueTag(public.cpp 中发挥作用- 它们不应该是 static

[编辑] 其他链接错误。

另外,在 public.h , 替换 static通过 extern

的声明中
extern int counter = 0;
extern int NamCounter = 0;
extern int TagCounter = 0;
extern vector <string> AllNames;
extern vector <string> AllTags;

并在 public.cpp 中实现它们

int counter = 0;
int NamCounter = 0;
int TagCounter = 0;
vector <string> AllNames;
vector <string> AllTags;

参见 extern here

关于C++ 出现 LNK2019 错误,即使没有循环依赖,也没有包含两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39205357/

相关文章:

C++ 静态数组初始化 : does inline initialization reserve space?

java - 使用 Java native 代码或 C++(QT、WxWidgets 等)进行独立于平台的编程

c++ - 模板基类的调用方法

windows - C++/msvc6 应用程序由于堆损坏而崩溃,有什么提示吗?

具有两个不同条件的 C++ do/while 循环

c++ - 开发 Windows 窗体应用程序?

c# - 我可以让控制台显示中文吗?

c# - 在 Visual Studio 中使用自定义方法时在 View 名称上显示红色下划线

c# - Visual Studio 任务窗口 - 等待与阻止的任务

c++:将来安排函数调用?