2016-11-27 22 views
-2

我對學校的分配相匹配:編譯錯誤:重載函數的多個實例的arument列表

i. Create a classical Guitar object with price $150 and type = 「classical」. Set the new price to $100 and display all the information about the Guitar object.

ii. Create an electric Guitar object with price $135 and type = 「electric」. Change the price as there is a promotion and display all the information about the Guitar object.

我試圖解決它在我自己的,但我在C新型++和我m停留在我無法理解的編譯器錯誤。

這裏是我在我的Guitar.h文件中創建的類。

#pragma once 
#include<iostream> 
#include <string> 
#include<sstream> 
using namespace std; 

class Guitar 
{ 
private: 
    string type; 
    double price; 
public: 
    Guitar(string type, double price); 
    string getType(); 
    double getPrice(); 
    void setPrice(double newPrice); 
    void setPrice(bool promotion); 
    string toString(); 
}; 

這是我Guitar.cpp文件中的類實現

#include "Guitar.h" 

Guitar::Guitar(string typeclass, double priceclass) 
{ 
    type = typeclass; 
    price = priceclass; 
} 
string Guitar::getType() 
{ 
    return type; 
} 
double Guitar::getPrice() 
{ 
    return price; 
} 
void Guitar::setPrice(double newPriceclass) 
{ 
    price = newPriceclass; 
} 
void Guitar::setPrice(bool promotion) 
{ 
    if (promotion == true) 
     price *= 0.9; 
} 
string Guitar::toString() 
{ 
    stringstream info; 
    info << "Guitar Type: " << type << endl 
     << "Price: " << price << endl; 
    return info.str(); 
} 

最後我有我的主文件GuitarApp.cpp

#include"Guitar.h" 

int main() 
{ 
    Guitar guitar1("Classical", 150.0); 
    guitar1.setPrice(100) << endl; 
    cout << guitar1.toString() << endl; 
    Guitar guitar2("Electrical", 135.0); 
    guitar2.setPrice(true); 
    cout << guitar2.toString() << endl; 
} 

我有2個錯誤:

  1. more than one instance of overloaded function Guitar::setPrice matches the argument list
  2. Guitar::setPrice ambiguous call to overloaded function.

有人可以向我解釋錯誤和我應該怎麼做來獲得代碼編譯?

編輯:已經改變100100.0後,我得到了4個誤區:

  1. mismatch in formal parameter list
  2. expression must have integral or unscoped enum type
  3. cannot determine which instance of function template std::endl ; is intended
  4. '<<': unable to resolve function overload

所有的錯誤都在我的GuitarApp.cpp的7號線是

guitar1.setprice(100.0)<<endl; 

如果我是編輯吉他的價格從100.0100,我會得到我最初的兩個錯誤。

+0

將100更改爲100.0。其他四個錯誤是什麼? –

+0

您的編譯器肯定會告訴錯誤的行號;你能完成嗎?順便說一句,現在有一個缺失的cout(因爲你之前沒有endl錯誤) – Christophe

+0

@Christophe所有的錯誤都在我的GuitarApp.cpp的第7行,它是 guitar1.setprice(100.0)<< endl ; – Kris

回答

3

文字100的類型是int。由於int可以很容易地轉換爲bool,因爲它是double,所以應該調用哪些函數是不明確的。

更改100100.0(一個double字面)應該解決這個問題。

+0

是的!我們甚至可以建議使用另一個函數名來激活促銷活動,例如promote()或setPromotion() – Christophe

+0

確實,真正的解決方案是更好地命名函數 – qxz

-1

通常我們並沒有在這裏修復家庭作品。最好問「爲什麼這行代碼不起作用」,而不是在這裏拋出一堆作業,希望能讓它從別人那裏做好準備......

也請給記住,你的問題是 「題外話」,是因爲:

Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers.

OK,你的代碼有如下的語法錯誤:

guitar1.setPrice(100.) ; // see the "." behind the number! 

你有方法:

void setPrice(double newPrice); 
void setPrice(bool promotion); 

而且你寫道:

guitar1.setPrice(100) 

100int而不是double而不是bool。因此,編譯器無法決定從您的100 a bool(其值爲true)或使其成爲double(值爲100.)。因此,只需添加一個點,使您的值成爲編譯器所需的浮點數double

下一個錯誤:

cout << guitar2.toString() << endl; // see the "2" behind guitar ! 

只有一個錯字...

一些言論:

分裂這樣一個類的頭和源文件是壞的!優化器沒有機會內聯函數。

使用using namespace std;可能不好!編寫std :: string要好得多,而且你需要從你的定義來自哪個命名空間。這會使工作稍微多些,但稍後閱讀會更好,特別是如果您使用多個庫中的多個名稱空間。

說明:

這是很容易保存在第一視圖中輸入一些字符。但是如果你以後(重新)在一個更大的應用程序中使用你的代碼,那麼你必須處理很多可能定義函數/類/與其他庫具有相同名稱的庫,你開始改變你的碼。

一個簡單的例子是讀posix並讀取istream。這裏給出':: read'來選擇沒有綁定到名稱空間的posix也是一個好主意。

是不是教條給予暗示不使用using namespace?我個人的經驗是,如果您使用它,如果您的代碼稍後在更大的應用程序中被重新使用,則可能會遇到問題。而對於它是強制性的給我寫代碼,這樣,(再)沒有問題,而且或將來很多收養/更正使用哪個可以是。

你必須決定:保存一些字符今天類型和以後可能會碰到麻煩或現在做的工作。

也許作品代碼是可以這樣做的。但我認爲談論這類代碼編寫中可能出現的問題是一個很好的觀點。

+1

*「Using using namespace std;'很糟糕!「*。不教條壞。只是以某種方式污染全局命名空間。如果名稱空間引用很多,在函數範圍內執行它沒有任何問題。 – StoryTeller

+0

@StoryTeller:http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – Klaus

+1

你仍然被教條。如果您遇到名稱衝突,您可以隨時消除歧義。我建議你考慮你喜歡看什麼: 'v.erase(std :: remove(std :: begin(v),std :: end(v),elem));'或'v.erase(remove (開始(a),結束(a),elem));'(爲了爭辯,我們忽略ADL)。 – StoryTeller

相關問題