C++ 奇怪的代码编译

C++ 奇怪的代码编译,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::Item(double Order

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

这是我的.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();
 };
#pragma一次
#包括
使用名称空间std;
类项目
{
私人:
字符串描述;
双倍价格;
整数权重;
整数;
公众:
项目(无效);
~项目(无效);
Item::Item(双订单价格、整数订单重量、字符串描述);
作废setOrderPrice(双倍金额);
无效设定订购重量(单位盎司);
void setDescription(字符串描述);
无效设置数量(整数);
int getOrderPrice();
int getOrderWeight();
字符串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;
 } 
#包括
#包括
#包括“项目h”
使用名称空间std;
项目::项目(无效)
{
}
Item::Item(双订单价格、整数订单重量、字符串描述)
{
}
项目::~项目(无效)
{
}
无效项::setOrderPrice(双倍金额){
价格=金额;
}
无效项::设置订单重量(整数盎司){
重量=盎司;
}
void Item::setDescription(字符串描述){
描述=描述;
}
无效项::设置数量(整数){
数量=数量;
}
int Item::getOrderPrice(){
退货价格;
}
int Item::getOrderWeight(){
返回重量;
}
字符串项::getDescription(){
返回说明;
}
int Item::getQuantity(){
退货数量;
}
无效项::显示(){
cout在您的.h文件中:

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

Item(double OrderPrice, int OrderWeight, string Description);
不需要限定第二个构造函数

另请注意:

int Item::getOrderPrice() {
  return price;
}
价格为
双精度
,您返回的是
int
。最后:

iTotalWeight += itmGlasses.getOrderPrice();
你在“体重”上加了一个“价格”——可能不是你想要的

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

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

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

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

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

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

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

您忘了告诉我们发生了什么错误。可能是由于(在类定义中)构造函数声明错误,导致了编译错误

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)
{}

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

为了在堆栈溢出方面获得好的结果,并帮助自己学习,关键的一步是创建可复制的小测试用例,清楚地说明您期望的内容和正在获得的内容

让我们减少您的代码:

#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::show()
的输出。这一行是原始代码中的第一个点,其中出现了与您期望不符的内容。这就是我在缩减代码时开始的地方。

那么“关闭”是什么呢关于它?当你没有提供你得到的错误信息时,你显然错了…它是用奇怪的数字编译的,而不是像我应该得到的那样的值。让你的代码编译是第一步。正确的逻辑通常更难。
#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;
}