2012-05-29 65 views
1

我一直在研究這個問題一段時間,但似乎無法弄清楚。我不能編譯代碼,因爲它一直拋出下面的錯誤。基本上,我試圖做的是使用指針創建一個包含來自基類和派生類的對象的向量。VS2010致命錯誤LNK1120:1個未解決的外部問題

Final Assignment.obj:error LNK2019:函數「public:__thiscall」中引用的無法解析的外部符號「public:__thiscall Product :: Product(void)」(?? 0Product @@ QAE @ XZ) ,class std :: basic_string,class std :: allocator>,int,double,double)「(?? 0SaleProduct @@ QAE @ DV?$ basic_string @ DU?$ char_traits @ D @ std @@ V?$ allocator @ D @ 2 @@ STD @@ HNN @ Z)
致命錯誤LNK1120:1周無法解析的外部

#include "stdafx.h" 
#include <iostream> 
#include <fstream> 
#include <iomanip> 
#include <vector> 
#include <string> 
using namespace std; 

class Product 
{ 


public: 

Product(); 
Product(char iT,string des,int inv,double p,double dis) 
{ 
invType = iT; 
description = des; 
inventory = inv; 
price = p; 
discount = dis; 
} 

char getInvType(){ 
return invType; 
} 

protected: 
char invType ; 
string description; 
int inventory; 
double price; 
double discount; 
}; 

class SaleProduct:public Product { 

public: 



//SaleProduct(); 
SaleProduct(char iT,string des,int inv,double p,double dis){ 


} 


}; 



int main() 
{ 
int choice = 0; 
// SaleProduct* SaleProductOB; 
// Product *productPoint = &ProductOB; 
vector<Product*> inventoryVec; 

    char invType; 
    string description; 
    int inventory; 
    double price; 
    double discount = 0; 




ifstream inFile("Inventory.txt"); 

if (inFile.is_open()){ 

while (inFile >> invType >> description >> inventory >> price >>discount){ 

     if (invType == 'S'){ 

      inventoryVec.push_back(new SaleProduct(invType,description,inventory,price,discount) ); 
    }else{ 
    //inventoryVec.push_back(new Product(invType,description,inventory,price,discount) ); 

     } 

} 
}else{ 
cout << "File is not ready!"; 
} 



cout << inventoryVec.size() << endl; 


while (choice != 3) { 
     cout << "Please enter your choice:" << endl; 
     cout << "1. Print available items" << endl; 
     cout << "2. Add item to cart"  << endl; 
     cout << "3. Print receipt and quit" << endl << endl; 

     cin >> choice; 
} 








//system("PAUSE"); 
return 0; 
} 
+0

如果您確實希望產品默認構造函數爲空,請將其設置爲空; Product(){}'。 –

+0

先生,你是個天才!如果我已經指定了一個構造函數,你知道爲什麼需要一個默認的構造函數嗎? – Dre

+0

你不需要一個,我想你試圖調用基類的非默認構造函數,但沒有語法來執行它。有關如何做到這一點,請參閱Mark的回答,並且您可以刪除默認的構造函數。 –

回答

2

您可能需要指定正確的構造函數(如,它試圖使用默認的構造函數,它具有沒有實際的定義):

SaleProduct(char iT,string des,int inv,double p,double dis) : 
    Product(iT, des, inv, p, dis){ 
+1

我想我明白它知道。因此,由於SaleProduct類派生自Product類,因此SaleProduct構造函數會嘗試使用Product構造函數? – Dre

+0

@AndresRubalcava是的,默認情況下它將使用默認(無參數)構造函數,上面是使用非默認構造函數構造基類的一種方式。 –

+0

@Mark Wilkins && Joachim Isaksson非常感謝您的幫助。我會在構造函數上做更多的閱讀。 – Dre

相關問題