C++ 链接器错误 LNK2001

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13634193/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:33:47  来源:igfitidea点击:

Linker error LNK2001

c++constructorlinkervisual-studio-2012

提问by Awia

When I try to create an object I get a LNK2001 error in Visual Studio, it's a problem with the constructor I think since changing the constructor changes the error.

当我尝试创建一个对象时,我在 Visual Studio 中收到 LNK2001 错误,我认为这是构造函数的问题,因为更改构造函数会更改错误。

Customer bob("Bob", "25 Bob Lane", "01bob82", "M", "bob/bob/bob");

This line gives this error:

这一行给出了这个错误:

Error   1   error LNK2001: unresolved external symbol "public: __thiscall
Customer::Customer(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> >,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> >,class std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char> >)" (??0Customer@@QAE@V?$basic_string@DU?$char_traits@D@std@@V?
$allocator@D@2@@std@@0000@Z)    D:\Dropbox\Work\C++\C++ Assignment\C++ 
Assignment\driver.obj

Customer class that contains the constructor:

包含构造函数的客户类:

#pragma once
#include "l_list.h"
#include "Account.h"
#include <string>

using namespace std;

class Customer
{
private:
    l_list<Account> accounts;
    string name;
    string address;
    string telNo;
    string sex;
    string dob;

public:
    Customer(string name, string address, string telNo, string sex, string dob)
    {
        Customer::name = name;
        Customer::address = address;
        Customer::telNo = telNo;
        Customer::sex = sex;
        Customer::dob = dob;
    }

    void createAccount()
    {
        cout << "What type of account?";

    }

};

回答by StahlRat

If you have linking error then syntactically your code is OK otherwise you'll get compiler errors.

如果你有链接错误,那么你的代码在语法上是可以的,否则你会得到编译器错误。

What you should check(or add) is path in Dependencies property of the project that uses Customer class. In VS you can find it "Project Properties->Configuration Properties->Linker->Input->Additional Dependencies". Seems that linker can't find the external library with Customer implementation. You can successfully compile your project cause all #include are correct but you fail on the stage of linking just because of dependencies.

您应该检查(或添加)的是使用 Customer 类的项目的 Dependencies 属性中的路径。在VS中你可以找到它“项目属性->配置属性->链接器->输入->附加依赖项”。似乎链接器找不到带有客户实现的外部库。您可以成功编译您的项目,因为所有 #include 都是正确的,但是由于依赖关系,您在链接阶段失败了。

回答by mark

What's there looks OK to me. Check other things, like make sure your namespaces are right, or there's not another/conflicting "Customer" definition, etc. Try commenting out large sections of code or reducing your code to a small test case.

那里有什么对我来说没问题。检查其他事项,例如确保您的命名空间正确,或者没有另一个/冲突的“客户”定义等。尝试注释掉大部分代码或将您的代码减少到一个小的测试用例。

回答by NVN

I encountered exact same problem. This is how I fixed:

我遇到了完全相同的问题。这就是我修复的方式:

Use #include<string>instead of #include "string.h"in the file calling Customerconstructor.

在调用构造函数的文件中使用#include<string>而不是。#include "string.h"Customer

回答by STF

I had the same error. It turned out that one necessary function was commented out. When I uncommented this function, the error disappeared.

我有同样的错误。原来,一个必要的功能被注释掉了。当我取消注释此函数时,错误消失了。