奇怪的代码编译 [英] Code Compiling Strangely

查看:142
本文介绍了奇怪的代码编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是我的.h文件

我认为我可以修复我的代码,所以它会编译, p>

  #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>
#includeItem.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<价格<重量<描述;
}

这是我的主要档案:

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

int main(){
double dTotalPrice = 0.0;
int iTotalWeight = 0;
项目itmMouse(24.99,14,无线鼠标);
Item itmKeyboard(22.49,27,USB Keyboard);
项目itmHDMI(24.99,12,HDMI电缆);
Item itmGlasses(7.99,7,Reading Glasses);
itmGlasses.setQuantity(2);
//使用printDetails()显示订单的详细信息
cout<< 这是你的购物车内容。
itmMouse.show();
itmKeyboard.show();
itmHDMI.show();
itmGlasses.show();
//计算本节中的总价格和总重量
dTotalPrice + = itmMouse.getOrderPrice();
dTotalPrice + = itmKeyboard.getOrderPrice();
dTotalPrice + = itmHDMI.getOrderPrice();
dTotalPrice + = itmGlasses.getOrderWeight();
iTotalWeight + = itmGlasses.getOrderPrice();
iTotalWeight + = itmKeyboard.getOrderWeight();
iTotalWeight + = itmHDMI.getOrderWeight();
iTotalWeight + = itmGlasses.getOrderWeight();
//这里我们显示订单详细信息
cout< 你的订单的价格是$< dTotalPrice<< endl;
cout< 运输重量是< iTotalWeight< ounces \\\
;
cout< 这是< iTotalWeight / 16< pounds\\\
;

return 0;

}

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



提前感谢!

解决方案

在您的.h档案中:

 code> 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(); 

您正在为您的体重添加价格 - 可能不是您想要的。 p>

最后,您不是将您的item()构造函数中的值存储在任何var中。请在item.cpp文件构造函数中使用初始化器列表:

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

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


I think that I was reasonably able to fix my code so it would compile, but something is still off about it.

This is my .h file

#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();
 };

This is my .cpp file:

#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;
 } 

This is my main file:

 #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;

 }

I am interested in knowing where I went wrong.

Thanks in advance!

解决方案

In your .h file:

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

Should be :

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

No need to qualify the second constructor.

Also note:

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

Price is a double and you are returning an int. Finally:

iTotalWeight += itmGlasses.getOrderPrice();

You are adding a "Price" to your "Weight" - probably not what you wanted.

Last, you are not storing your values from your item() constructor in any of your vars. use an initializer list in your item.cpp file constructor:

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

Compiler warnings/errors flagged all these issues for me...

这篇关于奇怪的代码编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆