2013-07-29 35 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) 
    { 
    }