我正在爲我的一個課程做一個項目,我很確定我幾乎完成了這個項目。基本上我必須輸入2人票,我必須找到最大和最小价格。我需要重載*和/運營商來解決該項目的問題。此外,通過老師的指示,friend
聲明是此項目的必要條件。「沒有可行的重載」='「爲什麼?
現在,爲這個問題。我試圖將適當的票據變量(t1或t2)存儲到t3中,以便我可以將它返回給主要。當我使用「=」來設置t1到t3時,它會顯示「no viable overloaded'='」。下面是我的代碼:
#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);
}
首先,發佈完整的錯誤並將樣本減少到複製錯誤所需的最小值。其次,「票證」不是「雙」,那麼如何分配「雙重」工作呢? – chris
我發佈了整個代碼,因爲有時錯誤不在您認爲它所在的區域。就是這樣。 – user3377191
如果你減少它,它仍然在同一行上給出相同的錯誤信息,這是一個很好的跡象表明問題仍然存在。 – chris