2013-07-29 37 views
-1

我很新的C++在這個題目對我來說有點太技術等等其他職位。當我嘗試編譯我的代碼時,出現未解析的外部符號錯誤。新到C++:未解決的錯誤

這裏是我的.h文件:

#pragma once 
#include <string> 
using namespace std; 

class Item 
{ 
private: 
    string description; 
    double price; 
    int weight; 
    int quantity; 

public: 
Item(void); 
~Item(void); 
Item(double OrderPrice, int OrderWeight, string Description); 
void setOrderPrice(double amount); 
void setOrderWeight(int ounces); 
void setDescription(string desc); 
void setQuantity(int number); 

int getOrderPrice(); 
int getOrderWeight(); 
string getDescription(); 
int getQuantity(); 

void show(); 
}; 

這是我的.cpp文件:

#include <iostream> 
#include <string> 
#include "Item.h" 
using namespace std; 

Item::Item(void) 
{ 
} 


Item::~Item(void) 
{ 
} 

void Item::setOrderPrice(double amount) { 
price = amount; 
} 

void Item::setOrderWeight(int ounces) { 
weight = ounces; 
} 

void Item::setDescription(string desc) { 
description = desc; 
} 

void Item::setQuantity(int number) { 
quantity = number; 
} 

int Item::getOrderPrice() { 
return price; 
} 

int Item::getOrderWeight() { 
return weight; 
} 

string Item::getDescription() { 
return description; 
} 

int Item::getQuantity() { 
return quantity; 
} 

void Item::show() { 
cout << price << weight << description; 
} 

最後包含文件我的主:

#include <iostream> 
#include <string> 
#include "Item.h" 
using namespace std; 

int main() { 
double dTotalPrice = 0.0; 
int iTotalWeight = 0; 
Item itmMouse(24.99, 14, "Wireless Mouse"); 
Item itmKeyboard(22.49, 27, "USB Keyboard"); 
Item itmHDMI (24.99, 12, "HDMI Cable"); 
Item itmGlasses(7.99, 7, "Reading Glasses"); 
itmGlasses.setQuantity(2); 
// Show the details of the order using printDetails() 
cout << "Here are your shopping cart contents.\n"; 
itmMouse.show(); 
itmKeyboard.show(); 
itmHDMI.show(); 
itmGlasses.show(); 
// Compute the total price and total weight in this section 
dTotalPrice += itmMouse.getOrderPrice(); 
dTotalPrice += itmKeyboard.getOrderPrice(); 
dTotalPrice += itmHDMI.getOrderPrice(); 
dTotalPrice += itmGlasses.getOrderWeight(); 
iTotalWeight += itmGlasses.getOrderPrice(); 
iTotalWeight += itmKeyboard.getOrderWeight(); 
iTotalWeight += itmHDMI.getOrderWeight(); 
iTotalWeight += itmGlasses.getOrderWeight(); 
// Here we show the order details 
cout << "The price of your order is $ " << dTotalPrice << endl; 
cout << "The shipping weight is " << iTotalWeight << " ounces\n"; 
cout << "That is " << iTotalWeight/16 << " pounds\n"; 

return 0; 
} 

我的錯誤得到的是:

error LNK2019: unresolved external symbol "public: __thiscall  Item::Item(double,int,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" ([email protected]@[email protected]?$basic_stri[email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@@Z) referenced in function _main 

請讓我知道(在非技術術語成爲可能),我在做什麼錯誤,以及如何解決它。預先感謝您的幫助!對此,我真的非常感激!

+4

你已經爲你的頭一個構造函數,你還沒有實現。 –

+2

不是你寫的功能相匹配的原型'項目(雙OrderPrice,INT OrderWeight,字符串描述);' – Nbr44

+1

如果你是很新的C++,那麼我會建議你去通過一個像樣的書和網上的參考徹底:) – 0decimal0

回答

9

作爲接頭說,你在你的.cpp文件無緣

Item(double OrderPrice, int OrderWeight, string Description); 

實施所以在.cpp添加

Item::Item(double OrderPrice, int OrderWeight, string Description) 
    { 
    // your initialization step.... 
    } 
+0

我想我已經做了正確的建議。這讓編譯器最終能夠工作,但是我得到了一個非常奇怪的結果,所以有些事情是不對的。 這是編譯器顯示: 這裏有您的購物車的內容。 -9.25596e + 061-858993460-9.25596e + 061-858993460-9.25596e + 061-858993460-9.25596e + 0 61-858993460您的訂單的價格是$ -7.30144e + 009 運輸重量是-429496732盎司 即-26843545磅 按任意鍵繼續。 。 。 – David

+0

其他問題,其他問題,請把你的整個代碼展示你如何使用這個類。現在該值看起來像非初始化變量! – alexbuisson

+0

這是我的.h文件: '#pragma once #include using namespace std; class Item { private: \t string description; \t雙重價格; \t int weight; \t int數量; public: \t Item(void); \t〜Item(void); \t Item :: Item(double OrderPrice,int OrderWeight,string Description); \t void setOrderPrice(double amount); \t void setOrderWeight(int盎司); \t void setDescription(string desc); \t void setQuantity(int number); \t int getOrderPrice(); \t int getOrderWeight(); \t string getDescription(); \t int getQuantity(); \t void show(); };' – David

0

提供的項目類作爲該參數的構造函數編譯器只能提供一個默認構造函數(Item()),並且如果需要使用默認值創建對象,則必須提供參數化對象,如同在main.cpp文件中所做的那樣。

0

你從來沒有真正定義了重載的構造函數,你在Itme.h聲明

無法解析的外部手段,它無法解決或者換句話說,找到函數調用或聲明

外部意味着它應該可以用另一種obj文件的某處定義 意義上的Visual Studio編譯到不同的.obj文件 每.cpp文件,然後鏈接它們放在一起,感覺你告訴它有一個名爲項目::項目功能 ...應該有是一個,但是如果 源文件被寫入,這並不容易你和它的項目的一部分,這將是很多 如果它是在一個圖書館女巫將意味着你還沒有鏈接 正確的圖書館很難 (就這樣你知道,因爲你會遇到這個像大家一樣)

解決這個剛纔添加的功能定義Item.cpp

 Item::Item(double OrderPrice, int OrderWeight, string Description) 
    { 
    }