Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:\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?";

    }

};
like image 716
Awia Avatar asked Nov 29 '12 20:11

Awia


People also ask

What does lnk2001 stand for?

Linker Error (LNK2001: unresolved external symbol DllCanUnloadNow,DllGetClassObject,DllRegisterServer,DllUnregisterServer) with IntelC++ Compiler, no ... - Intel Communities This community is designed for sharing of public information.

Why does the linker generate an error when trying to resolve?

The linker generates an error when it can't resolve an external symbol. It means the linker couldn't find a matching exported symbol definition in any of the linked files. When the project is missing a reference to a library (.LIB) or object (.OBJ) file.

What are the causes of lnk2001?

Linking them can cause error LNK2001. Mixing inline and non-inline compile options on different modules can cause LNK2001. If a C++ library is created with function inlining turned on (/Ob1 or /Ob2) but the corresponding header file describing the functions has inlining turned off (no inline keyword), this error occurs.

What is a link error in C++?

A link error can result if the declaration of a function or variable doesn't exactly match the definition of the function or variable. That's because any difference becomes part of the symbol name to match. The error can happen even if the same header file is used in both the calling code and the defining code.


2 Answers

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.

like image 175
StahlRat Avatar answered Sep 20 '22 11:09

StahlRat


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.

like image 28
mark Avatar answered Sep 21 '22 11:09

mark