-4

我认为我能够合理地修复我的代码以便它可以编译,但它仍然存在一些问题。

这是我的 .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;

 }

我有兴趣知道我哪里出错了。

提前致谢!

4

4 回答 4

4

在您的 .h 文件中:

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

应该 :

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

无需限定第二个构造函数。

另请注意:

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

价格是 adouble而你返回的是int。最后:

iTotalWeight += itmGlasses.getOrderPrice();

您正在为您的“重量”添加“价格” - 可能不是您想要的。

最后,您没有将 item() 构造函数中的值存储在任何变量中。在 item.cpp 文件构造函数中使用初始化列表:

Item::Item(double OrderPrice, int OrderWeight, string Description):
    description(Description),
    price(OrderPrice),
    weight(OrderWeight),
    quantity(1)
...

编译器警告/错误为我标记了所有这些问题......

于 2013-07-29T17:11:22.280 回答
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)
{}

删除默认构造函数也是一个好主意,这样用户就不会意外创建未初始化的对象。

于 2013-07-29T17:16:28.243 回答
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(). 该行是您的原始代码中的第一个点,其中出现了您所不期望的东西。当我减少你的代码时,这就是我开始的地方。

于 2013-07-29T17:49:14.823 回答
0

好的,你的构造函数没有初始化类成员

   Item::Item() : description(""), price(0), weight(0), quantity(0)
   {}

   item::Item(double OrderPrice, int OrderWeight, string Description) :
    description(Description),
    price(OrderPrice),
    .... etc....
     {}

因此,您对“getter”的所有调用都将返回未初始化的值。

于 2013-07-29T17:13:42.423 回答