Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ &引用;没有可行的过载'='&引用;为什么?_C++ - Fatal编程技术网

C++ &引用;没有可行的过载'='&引用;为什么?

C++ &引用;没有可行的过载'='&引用;为什么?,c++,C++,我正在为我的一门课做一个项目,我很确定我几乎完成了这个项目。基本上,我必须输入两个人的票,我必须找到最高和最低价格。我需要重载*和/操作员来解决项目中的问题。此外,根据老师的指示,friend声明是本项目的必要条件 现在,我来谈谈这个问题。我试图将正确的票证变量(t1或t2)存储到t3中,以便将其返回到main。当我使用“=”将t1设置为t3时,它会显示“没有可行的重载“=”。以下是我的代码: #include <iostream> using namespace std; cl

我正在为我的一门课做一个项目,我很确定我几乎完成了这个项目。基本上,我必须输入两个人的票,我必须找到最高和最低价格。我需要重载*和/操作员来解决项目中的问题。此外,根据老师的指示,
friend
声明是本项目的必要条件

现在,我来谈谈这个问题。我试图将正确的票证变量(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);
}
#包括
使用名称空间std;
班票
{
公众:
票证();
双输入();
双输出();
朋友票操作员*(常数票和t1、常数票和t2);
朋友票接线员/(常数票和t1、常数票和t2);
私人:
无效成本();
字符串名;
双倍里程,价格;
国际转让;
};
int main()
{
客票客户1、客户2、客户3;
//------------------------------------------------

您不能为
票证
定义以double作为参数的
运算符=
。因此,您不能将double分配给
票证
对象。

您可能需要使用成员函数

设置价格(常数和双倍价格)

因此,您可以像这样更改错误代码,这会更好

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

这就是我如何得到相同编译错误的原因。我已将一个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; 
    }
}
在这种情况下,我们有两种解决方案:

1) 在函数定义中删除
const
修饰符;

2) 将属性
m_extension
标记为
mutable

首先,发布完整的错误,并将样本减少到复制错误所需的最小值。其次,
票据
不是
双精度
,那么分配
双精度
如何工作呢?我发布了整个代码,因为有时会出现错误不在您认为它所处的区域内。仅此而已。如果您缩小它,它仍然在同一行上给出相同的错误消息,这很好地表明问题仍然存在。实际上,赋值仍然通过编译器提供的运算符工作,因此赋值其他
ticket
对象将正常工作。