当前位置: 动力学知识库 > 问答 > 编程问答 >

c++ - Linker error LNK2001

问题描述:

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.

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> >)" ([email protected]@[email protected][email protected][email protected]@[email protected]@V?

[email protected]@[email protected]@[email protected]@[email protected]) 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?";

}

};

网友答案:

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.

网友答案:

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.

网友答案:

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

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

网友答案:

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

分享给朋友:
您可能感兴趣的文章:
随机阅读: