2013-07-29 162 views
-3

我認爲我是相當能解決我的代碼,以便它會編譯,但事情仍然關閉它。代碼編譯奇怪

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

那麼它有什麼「關」呢? – Daniel

+10

當你沒有提供你得到的錯誤信息時,你顯然出錯了...... – ppeterka

+0

它用奇怪的數字編譯而不是類似我應該得到的值。 – David

回答

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

您正在爲「重量」添加「價格」 - 可能不是您想要的。

最後,你是不是從你的物品存儲你的價值觀在任何您的瓦爾()構造函數。在item.cpp文件的構造使用初始化列表:

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

編譯器警告/錯誤標記所有這些問題對我來說...

0

好了,你的構造函數不初始化類成員

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

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

所以所有的「吸氣」調用將返回未初始化值。

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

它也可能是一個好主意,刪除默認的構造函數,以便用戶不會意外創建一個未初始化的對象。

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()輸出斷言的。這條線是您的原始代碼中的第一個出現問題的地方,這不符合您的期望。那就是我縮減代碼的時候開始的地方。