Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
211 views
in Technique[技术] by (71.8m points)

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> >)" (??0Customer@@QAE@V?$basic_string@DU?$char_traits@D@std@@V?
$allocator@D@2@@std@@0000@Z)    D:DropboxWorkC++C++ AssignmentC++ 
Assignmentdriver.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?";

    }

};
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...