Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"No viable overloaded '=' " why?

Tags:

c++

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 friend declaration is a necessity for this project via teacher's instructions.

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:

#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);
}
like image 623
user3377191 Avatar asked Mar 04 '14 01:03

user3377191


3 Answers

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

like image 94
sepp2k Avatar answered Nov 12 '22 20:11

sepp2k


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

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 const modifier in the function definition;
2) mark the property m_extension as mutable.

like image 5
Yuchen Avatar answered Nov 12 '22 18:11

Yuchen


you might want to have member function

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);
like image 1
michaeltang Avatar answered Nov 12 '22 19:11

michaeltang