Kris Kris - 10 days ago 5
C++ Question

Compilation error: More than one instance of overloaded function matches the arument list

I have an assignment for school:


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.


I am trying to solve it on my own, but I am new in C++ and I'm stuck with compiler errors that I can't understand.

Here the class that I have created in my Guitar.h file.

#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 the class implementation in my Guitar.cpp 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();
}


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 and what I should do to get the code compiled?

Edit: After having changed
100
to
100.0
, I got 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




All errors are on line 7 of my GuitarApp.cpp which is

guitar1.setprice(100.0)<<endl;


If i were to edit the price of the guitar from
100.0
back to
100
, I would get the two error that I initially had.

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.