要调用的C ++不匹配函数

时间:2019-02-28 01:41:37

标签: c++ function

我可以知道密码有什么问题吗?

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

执行后,出现此错误

  

错误:没有匹配的函数可调用'Vehicle :: setPrice()'|

4 个答案:

答案 0 :(得分:2)

我发现一些错误:

using namespace std;

摆脱使用它的习惯。参见Why is "using namespace std" considered bad practice?

double comparePrices(const Vehicle&, Vehicle&);

comparePrices()是非static的方法,这意味着您需要在Vehicle对象上调用它,并传入两个Vehicle对象作为输入参数。这有点浪费和多余。将comparePrices()设为静态,或者删除Vehicle参数之一,然后使用隐式this参数访问被调用的对象。

此外,仅从设计角度来看,comparePrices()不应提示用户输入。该工作在调用main()之前属于comparePrices()

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

setPrice()double作为输入,但是您没有传递double值。这就是编译器错误所抱怨的。

此外,setPrice()没有返回值,其返回类型为void,因此您不能将其传递给operator<<。您需要向getPrice()类添加一个单独的Vehicle方法以返回其price成员。

话虽如此,请尝试以下方法:

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

或者这个:

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

答案 1 :(得分:0)

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

此行出错

setPrice()的返回类型为 void

您应该制作 Vehicle :: getPrice()之类的东西,以获取Price的价值。

答案 2 :(得分:0)

setPrice带一个double作为参数,但您不带参数调用它

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

因此功能签名不匹配。

答案 3 :(得分:0)

cout <<“二手车价格为$” << usedCar.setPrice();

相反,只需添加访问器方法getPrice() 只是返回汽车的价格。那应该可以了。 setPrice()需要传入一个参数,但是当您退出它时,您什么也不给。