2014-03-04 51 views
4

我正在爲我的一個課程做一個項目,我很確定我幾乎完成了這個項目。基本上我必須輸入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); 
} 
+2

首先,發佈完整的錯誤並將樣本減少到複製錯誤所需的最小值。其次,「票證」不是「雙」,那麼如何分配「雙重」工作呢? – chris

+0

我發佈了整個代碼,因爲有時錯誤不在您認爲它所在的區域。就是這樣。 – user3377191

+0

如果你減少它,它仍然在同一行上給出相同的錯誤信息,這是一個很好的跡象表明問題仍然存在。 – chris

回答

4

你不定義ticketoperator=採用一個雙作爲其參數。因此,您不能將雙打分配給ticket對象。

+1

實際上,賦值仍然通過編譯器提供的操作符工作,所以分配其他的「票據」對象將工作正常。 – chris

+0

@chris當然。我的錯。 – sepp2k

0

你可能希望有成員函數

set_price(const& double price)

,因此您可以更改錯誤這樣的代碼,這將是更好的

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

不直接相關的問題,但這個是我如何得到相同的編譯錯誤。

我已經標記了我的一個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; 
    } 
} 

在這種情況下必須要麼下降const改性劑或標記m_extension作爲mutable

相關問題