我對學校的分配相匹配:編譯錯誤:重載函數的多個實例的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個錯誤:
- more than one instance of overloaded function
Guitar::setPrice
matches the argument listGuitar::setPrice
ambiguous call to overloaded function.
有人可以向我解釋錯誤和我應該怎麼做來獲得代碼編譯?
編輯:已經改變100
到100.0
後,我得到了4個誤區:
- mismatch in formal parameter list
- expression must have integral or unscoped enum type
- cannot determine which instance of function template
std::endl
; is intended- '<<': unable to resolve function overload
所有的錯誤都在我的GuitarApp.cpp的7號線是
guitar1.setprice(100.0)<<endl;
如果我是編輯吉他的價格從100.0
回100
,我會得到我最初的兩個錯誤。
將100更改爲100.0。其他四個錯誤是什麼? –
您的編譯器肯定會告訴錯誤的行號;你能完成嗎?順便說一句,現在有一個缺失的cout(因爲你之前沒有endl錯誤) – Christophe
@Christophe所有的錯誤都在我的GuitarApp.cpp的第7行,它是 guitar1.setprice(100.0)<< endl ; – Kris