0

May I know what is wrong with the codes?

#include <iostream>

using namespace std;

class Vehicle
{
    double price = 0;
public:
    void setPrice(double p)
    {
        price = p;
    }
    double comparePrices(const Vehicle&, Vehicle&);
};

double Vehicle::comparePrices(const Vehicle& car1, Vehicle& car2)
{
    cout << "Enter the price for the used car: ";
    double price;
    cin >> price;
    car2.setPrice(price);
    return car1.price - car2.price;
}

int main()
{
    Vehicle newCar, usedCar;
    double diff;
    newCar.setPrice(38000);
    diff = newCar.comparePrices(newCar, usedCar);
    cout << "Used car price is $" << usedCar.setPrice();
    cout << ", the difference is $" << diff << endl;
}

After execution, I am getting this error

error: no matching function for call to 'Vehicle::setPrice()'|

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 2
    `setPrice` requires a value be provided. `void setPrice(**double p**)` Note that it also returns `void`. You cannot send `void` to `cout`. Do you mean to have a `getPrice` method? – user4581301 Feb 28 '19 at 01:44
  • 1
    *May I know what is wrong with the codes?* – It's unreadable because it isn't properly indented? – Swordfish Feb 28 '19 at 01:46
  • @Swordfish Better? – user4581301 Feb 28 '19 at 01:48
  • @user4581301 Yes, now it's better. – Swordfish Feb 28 '19 at 01:54
  • 1
    I wouldn't expect `Vehicle::comparePrices()` to ask for user input. It should be a free function. When it's a member i'd expect it to compare the price of a car provided as argument to the price of the car the function is called upon. – Swordfish Feb 28 '19 at 01:55

4 Answers4

2

I see a few things wrong:

using namespace std;

Get out of the habit of using that. See Why is "using namespace std" considered bad practice?.

double comparePrices(const Vehicle&, Vehicle&);

comparePrices() is a non-static method, which means you need to call it on a Vehicle object, as well as pass in two Vehicle objects as input parameters. That is a bit wasteful and redundant. Either make comparePrices() static, or else remove one of the Vehicle parameters and use the implicit this parameter to access the object being called on.

Also, just from a design perspective, comparePrices() should not be prompting the user for input. That job belongs in main() before calling comparePrices().

cout << "Used car price is $" << usedCar.setPrice();

setPrice() takes a double as input, but you are not passing in a double value. That is what the compiler error is complaining about.

Also, setPrice() does not have a return value, its return type is void, so you can't pass it to operator<<. You need to add a separate getPrice() method to the Vehicle class to return its price member.

With that said, try this instead:

#include <iostream>

class Vehicle
{
private:
    double price = 0;
public:
    double getPrice() const
    {
        return price;
    }

    void setPrice(double p)
    {
        price = p;
    }

    static double comparePrices(const Vehicle&, const Vehicle&);
};

double Vehicle::comparePrices(const Vehicle& v1, const Vehicle& v2)
{
    return v1.price - v2.price;
}

int main()
{
    Vehicle newCar, usedCar;
    double price, diff;

    newCar.setPrice(38000);

    std::cout << "Enter the price for the used car: ";
    std::cin >> price;
    usedCar.setPrice(price);

    diff = Vehicle::comparePrices(newCar, usedCar);
    std::cout << "Used car price is $" << usedCar.getPrice();
    std::cout << ", the difference is $" << diff << std::endl;

    return 0;
}

Or this:

#include <iostream>

class Vehicle
{
private:
    double price = 0;
public:
    double getPrice() const
    {
        return price;
    }

    void setPrice(double p)
    {
        price = p;
    }

    double comparePrices(const Vehicle&);
};

double Vehicle::comparePrices(const Vehicle& other)
{
    return price - other.price;
}

int main()
{
    Vehicle newCar, usedCar;
    double price, diff;

    newCar.setPrice(38000);

    std::cout << "Enter the price for the used car: ";
    std::cin >> price;
    usedCar.setPrice(price);

    diff = newCar.comparePrices(usedCar);
    std::cout << "Used car price is $" << usedCar.getPrice();
    std::cout << ", the difference is $" << diff << std::endl;

    return 0;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
0
cout << "Used car price is $" << usedCar.setPrice();

this line makes errors

return type of setPrice() is void

you should make things like Vehicle::getPrice() to get value of Price.

0

setPrice takes a double as a parameter but you called it without a parameter

cout << "Used car price is $" << usedCar.setPrice()

so the function signature doesn't match.

Steve
  • 366
  • 2
  • 14
0

cout << "Used car price is $" << usedCar.setPrice();

Instead of this, just add an accessor method getPrice() that just returns the price of the car. It should work then. setPrice() requires a parameter to be passed in but you aren't giving it anything when you cout it.

Aaron
  • 1
  • 3