C++ “没有可行的重载'='”为什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22161374/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:58:18  来源:igfitidea点击:

"No viable overloaded '=' " why?

c++

提问by user3377191

I'm making a project for one of my classes and I'm pretty sure I'm almost done with the project. Basically I have to input 2 people's tickets and I have to find the maximum and minimum price. I need to overload the * and / operators to fix that issue for the project. Also, the frienddeclaration is a necessity for this project via teacher's instructions.

我正在为我的一个班级制作一个项目,我很确定我几乎完成了这个项目。基本上我必须输入2人的票,我必须找到最高和最低价格。我需要重载 * 和 / 运算符来解决该项目的问题。此外,friend通过教师的指示,该项目的声明是必要的。

Now, for the issue. I'm trying to store the proper ticket variable (either t1 or t2) into t3 so I can return that to the main. When I use "=" to set either t1 to t3, it says "no viable overloaded '='". The following is my code:

现在,对于这个问题。我正在尝试将正确的票证变量(t1 或 t2)存储到 t3 中,这样我就可以将其返回到 main。当我使用“=”将 t1 设置为 t3 时,它说“没有可行的重载 '='”。以下是我的代码:

#include <iostream>

using namespace std;

class ticket
{
public:
    ticket();
    double input();
    double output();
    friend ticket operator *(const ticket &t1, const ticket &t2);
    friend ticket operator /(const ticket &t1, const ticket &t2);
private:
    void cost();
    string name;
    double miles, price;
    int transfers;
};


int main()
{
    ticket customer1, customer2, customer3;

    //------------------------------------------------
    cout << "*** Customer 1 ***" << endl;
    customer1.input();
    cout << "--- Entered, thank you ---" << endl;


    cout << "*** Customer 2 ***" << endl;
    customer2.input();
    cout << "--- Enter, thank you ---" << endl;
    //------------------------------------------------



    //------------------------------------------------
    cout << "Testing of the * operator: " << endl;
    customer3 = customer1 * customer2;
    cout << "*** Database printout: ***" << endl;
    customer3.output();
    cout << endl;
    cout << "--- End of Database ---" << endl;
    //------------------------------------------------


    //------------------------------------------------
    cout << "Testing of the / operator:" << endl;
    customer3 = customer1 / customer2;
    cout << "*** Database printout: ***" << endl;
    customer3.output();
    cout << endl;
    cout << "--- End of Database ---" << endl;
    //------------------------------------------------


        return 0;
}

ticket operator *(const ticket &t1, const ticket &t2)
{
    ticket t3;

    if (t1.price > t2.price)
        t3 = t1.price;
    else
        t3 = t2.price;
    return t3;
}
ticket operator /(const ticket &t1, const ticket &t2)
{
    ticket t3;

    if (t1.price < t2.price)
        t3 = t1.price;
    else
        t3 = t2.price;
        return t3;
}
ticket::ticket()
{
}

double ticket::input()
{
    cout << "Miles? ";
    cin >> miles;

    cout << endl << "Transers? ";
    cin >> transfers;

    cout << endl << "Name? ";
    cin >> name;

    cost();

    cout << endl << "Price is: " << price << endl;

    return miles;
}

double ticket::output()
{
    cout << name << '\t' << miles << "mi \t " << transfers << " transfers \t" << price;
    return miles;
}

void ticket::cost()
{
    price = (.5 * miles) - (50 * transfers);
}

回答by sepp2k

You don't define an operator=for ticketthat takes a double as its argument. As a consequence you can't assign doubles to ticketobjects.

您没有定义一个以 double 作为参数的operator=for ticket。因此,您不能为ticket对象分配双精度值。

回答by Yuchen Zhong

This is how I get the same compile error. I have mark one of my getter function as constwhile I still want to modify class member variable. See the following simple example:

这就是我得到相同编译错误的方式。我已经标记了我的 getter 函数之一,因为const我仍然想修改类成员变量。请参阅以下简单示例:

class CPath {
private: 
    std::string m_extension; 
public: 
    std::string GetExtension() const {
        if (m_extension.length()==0) {
            m_extension = "txt";
        }
        return m_extension; 
    }
}

In this case, we have two solutions:

在这种情况下,我们有两种解决方案:

1) drop constmodifier in the function definition;
2) mark the property m_extensionas mutable.

1)const函数定义中的drop修饰符;
2) 将该属性标记m_extensionmutable

回答by michaeltang

you might want to have member function

你可能想要成员函数

set_price(const& double price)

set_price(const& double price)

so you can change the error code like this,which would be better

所以你可以像这样更改错误代码,这会更好

 if (t1.price > t2.price)
        t3.set_price(t1.price);
    else
        t3.set_price(t2.price);