我认为我能够合理地修复我的代码以便编译,但有些事情仍然存在。
这是我的.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::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(double OrderPrice, int OrderWeight, string Description)
{
}
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;
}
我有兴趣知道我哪里出错了。
提前致谢!
答案 0 :(得分:4)
在你的.h文件中:
Item::Item(double OrderPrice, int OrderWeight, string Description);
应该是:
Item(double OrderPrice, int OrderWeight, string Description);
无需限定第二个构造函数。
另请注意:
int Item::getOrderPrice() {
return price;
}
价格为double
,您将返回int
。最后:
iTotalWeight += itmGlasses.getOrderPrice();
您正在添加&#34; Price&#34;你的体重&#34;体重&#34; - 可能不是你想要的。
最后,您不会在任何变量中存储item()构造函数中的值。在item.cpp文件构造函数中使用初始化列表:
Item::Item(double OrderPrice, int OrderWeight, string Description):
description(Description),
price(OrderPrice),
weight(OrderWeight),
quantity(1)
...
编译器警告/错误标记了所有这些问题......
答案 1 :(得分:2)
你忘了告诉我们出了什么问题。也许你得到一个编译错误,因为(在类定义中)构造函数声明
Item::Item(double OrderPrice, int OrderWeight, string Description);
应该只是
Item(double OrderPrice, int OrderWeight, string Description);
或者它可能为你编译(因为一些编译器接受了这个错误),但是你会得到奇怪的结果。这是因为该构造函数不初始化成员,因此它们具有垃圾值。也许你想要:
Item::Item(double OrderPrice, int OrderWeight, string Description) :
description(Description),
price(OrderPrice),
weight(OrderWeight),
quantity(1)
{}
删除默认构造函数也是一个好主意,这样用户就不会意外地创建一个未初始化的对象。
答案 2 :(得分:0)
好的,你的构造函数没有初始化类成员
Item::Item() : description(""), price(0), weight(0), quantity(0)
{}
item::Item(double OrderPrice, int OrderWeight, string Description) :
description(Description),
price(OrderPrice),
.... etc....
{}
因此,您对“getter”的所有调用都将返回未初始化的值。
答案 3 :(得分:0)
为了在堆栈溢出上获得良好的结果,并帮助自己学习,关键的一步是创建小型可重现的测试用例,清楚地说明您的期望,以及您获得的内容。
让我们减少您的代码:
#include <iostream>
#include <string>
#include <cassert>
using namespace std;
class Item
{
private:
string description;
double price;
int weight;
int quantity;
public:
Item(double OrderPrice, int OrderWeight, string Description);
int getOrderPrice();
};
Item::Item(double OrderPrice, int OrderWeight, string Description)
{
}
int Item::getOrderPrice() {
return price;
}
int main() {
Item itmMouse(24.99, 14, "Wireless Mouse");
assert(itmMouse.getOrderPrice() == 24.99);
}
现在,对于任何看过这段代码的人来说,(并且你应该包括一个关于这个的说明),很明显,你的价格是错误的。在这一点上,我们可以清楚地说明问题是你的构造函数没有将它的参数复制到类成员。
可能的修复方法是构造函数,如下所示:
Item::Item(double OrderPrice, int OrderWeight, string Description)
{
price = OrderPrice;
weight = OrderWeight;
description = Description;
quantity = 1;
}
我们也可以只查看Item::show()
的输出,而不是我正在使用的断言。该行是原始代码中的第一个出现不符合预期的内容。这是我在减少代码时开始的地方。