简体   繁体   English

LNK2019 - 未解决的外部符号错误

[英]LNK2019 - unresolved external symbol error

I have a problem when compiling my main class.编译主类时遇到问题。

//main.cpp    
#include "Division.h"

int main() {
    Division Root;
}

Here is my Division.h这是我的 Division.h

//Division.h
#pragma once
#include <string>
#include <windows.h>

class Division {
public:
    Division();
    Division(std::string Name, std::string PhoneNumber, std::string Description, Division& Parent);
private:
    UUID GUID;
    std::string Name;
    std::string PhoneNumber;
    std::string Description;
    Division* Parent;
};

And here is my Division.cpp这是我的 Division.cpp

#include <string>                                                                           
#include <windows.h>                                                                            

using namespace std;                                                                            

class Division {
public:
    Division() {};

    Division(string Name, string PhoneNumber, string Description, Division &Parent) {       
        UuidCreate(&GUID);                                                                  
        this->Name = Name;
        this->PhoneNumber = PhoneNumber;
        this->Description = Description;
        this->Parent = &Parent;
    }
    

private:
    UUID GUID;                                                                              
    string Name;
    string PhoneNumber;
    string Description;
    Division* Parent;
};

In the main class I just want to create an empty Division object using the first constructor.在主类中,我只想使用第一个构造函数创建一个空的 Division 对象。 Why am I getting the Linker error?为什么我收到链接器错误? What am I doing wrong?我究竟做错了什么?

You have declared Division twice, both in header and cpp file.您已在头文件和 cpp 文件中两次声明 Division。

You should only have the declaration in the header.您应该只在标题中声明。

In the .cpp file include the header and define the methods在 .cpp 文件中包含标题并定义方法

Division::Division() {}

Division::Division(std::string name, std::string phoneNumber, std::string description, Division &parent) {       
  UuidCreate(&GUID);                                                                  
  Name = name;
  PhoneNumber = phoneNumber;
  Description = description;
  Parent = &parent;
}

It is also good to use other names for member variables than parameters.对成员变量使用其他名称而不是参数也很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM