Kris Kris - 12 days ago 6
C++ Question

More than one instance of overloaded function c++

I have an assignment for school where I have to create a guitar object with price of $150 and the type is "classical". I am still new in C++ and thus I am very lost about this.


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.


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


This is my Guitar.h file.

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


This is my Guitar.cpp file

Finally I have my main file 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;
}


I have 2 errors.


  1. more than one instance of overloaded function "Guitar::setPrice" matches the argument list:

  2. Guitar::setPrice ambiguous call to overloaded function.



Can someone explain to me the errors?

Edit:
After changing

100 to 100.0


I had 4 more errors.


  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



Sorry If i had posted in the wrong thread/section.

All of the error is on line 7 of my GuitarApp.cpp which is

guitar1.setprice(100.0)<<endl;


If i were to edit the price of the guitar to

guitar1.setprice(100)<<endl;


I would get the two error that I initially had which is

more than one instance of overloaded function "Guitar::setPrice" matches the argument list:


and

Guitar::setPrice ambiguous call to overloaded function

qxz qxz
Answer

The type of the literal 100 is int. Since int is just as easily convertible to bool as it is to double, it's ambiguous which of those functions should be called.

Changing 100 to 100.0 (a double literal) should fix this.