2012-11-29 58 views
6

當我嘗試創建一個對象時,在Visual Studio中出現LNK2001錯誤,這是構造函數的問題,我認爲因爲更改構造函數會更改錯誤。鏈接器錯誤LNK2001

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

此行給出了這樣的錯誤:

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類包含構造函數:

#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?"; 

    } 

}; 
+0

這通常意味着您的頭文件包含錯誤版本或與錯誤版本的運行時庫鏈接。您的INCLUDE和LIB路徑應該指向相同的SDK版本。 – Flot2011

+0

這是一個鏈接器錯誤。所以,看起來你的項目不包含'Customer.cpp'。或者你正在通過舊的(或錯誤的)庫使用'class Customer'。 – qehgt

回答

2

那裏的東西看起來不錯給我。檢查其他事情,比如確保你的命名空間是正確的,或者沒有另外的/衝突的「客戶」定義等等。嘗試評論大部分代碼或者將你的代碼減少到一個小的測試用例。

+0

你確定這樣嗎?由於他調用了Customer :: name,這看起來應該是一個靜態引用,但事實並非如此。如果我正確閱讀,他應該調用Customer(string _name),然後使用_name調用init名稱。 – SinisterMJ

+0

是的沒關係,這基本上是說使用'客戶'對象的命名空間......雖然我個人不會那樣做,我會使用'this-> name = name'或者像'Customer(string名稱...):名稱(名稱)... {}' – mark

6

如果你有鏈接錯誤,那麼語法上你的代碼是確定的,否則你會得到編譯器錯誤。

您應該檢查(或添加)的內容是使用Customer類的項目的Dependencies屬性中的路徑。在VS中你可以找到它「Project Properties-> Configuration Properties-> Linker-> Input-> Additional Dependencies」。似乎鏈接器無法找到客戶實現的外部庫。你可以成功地編譯你的項目,因爲所有的#include都是正確的,但是你僅僅因爲依賴關係而失敗了。

1

我遇到了完全相同的問題。這是我如何修復:

使用#include<string>而不是#include "string.h"在調用Customer構造函數的文件中。

1

我有同樣的錯誤。事實證明,一個必要的功能被註釋掉了。當我取消註釋此功能時,錯誤消失。