2016-04-22 105 views
0

新的C++獲得一個未定義的引用爲<功能

的運營商在開始之前是的,這是家庭作業,平時我可以對我自己好嗎理出頭緒,但我沒有具體的得到任何實際的錯誤所以這個對我來說很難。

我的代碼:

#include <string> 
#include <iostream> 
using namespace std; 

class JobBid{ 

    private: 
     friend ostream& operator<<(ostream&, const JobBid&); 
     int bid; 
     int quote; 



    public: 
     JobBid& operator() (int, int); 
     bool operator<(const smallest) 
}; 

ostream& operator<<(ostream& out, const JobBid& baq){ 

    out << "Bid #: " << baq.bid << " Quote $: " << baq.quote << endl; 
    return out; 
} 

JobBid& JobBid::operator()(int b, int q){ 

    bid = b; 
    quote = q; 
} 



int main() 
{ 

    int b1; 
    int b2; 
    int b3; 
    int b4; 
    int q1; 
    int q2; 
    int q3; 
    int q4; 

    int smallestbid; 
    int smallestquote; 

    const int size = 4; 
    JobBid JBArray[size]; 

    cout << "Enter the bid number for the first bid:" << endl; 
    cin >> b1; 

    cout << "Now enter the quote price for the first bid:" << endl; 
    cin >> q1; 

    cout << "Enter the bid number for the second bid:" << endl; 
    cin >> b2; 

    cout << "Now enter the quote price for the second bid:" << endl; 
    cin >> q2; 

    cout << "Enter the bid number for the third bid:" << endl; 
    cin >> b3; 

    cout << "Now enter the quote price for the third bid:" << endl; 
    cin >> q3; 

    cout << "Enter the bid number for the fourth bid:" << endl; 
    cin >> b4; 

    cout << "Now enter the quote price for the fourth bid:" << endl; 
    cin >> q4; 

    JBArray[1](b1, q1); 
    JBArray[2](b2, q2); 
    JBArray[3](b3, q3); 
    JBArray[4](b4, q4); 

    cout << JBArray[1] << JBArray[2] << JBArray[3] << JBArray[4] << endl; 

    JobBid smallest = JBArray[0] ; 
    for (int i=1; i < sizeof(JBArray)/sizeof(JBArray[0]); ++i) 
     if (JBArray[i] < smallest) 
      smallest = JBArray[i] ; 

    cout << smallest << '\n' ; 

} 

我知道代碼可能是非常糟糕的,但它的前奏類。無論哪種方式,我試圖返回我創建的數組列表的最小值在主的末尾。然而,我發現試圖在我的類型'JobBid'上使用'<'操作符創建了錯誤,所以我環顧四周,發現你必須自己定義它。

我試圖使這裏做:

bool operator<(const smallest) 

,但我必須這樣做是錯誤的,我能得到所有的錯誤出在那之後(具體的線路我的意思是錯誤的),只是現在我就在這:

undefined reference to `JobBid::operator<(JobBid)' 

我不知道如何解決它。我認爲查找數組中最小對象的邏輯是正確的,所以它必須是小於號的東西。

+1

你有重載操作符的定義嗎? – FCo

+0

我想我不會,我會如何定義它?我想首先考慮的是最小的參數嗎?我想我需要做這樣的事情: JobBid&operator <(){ //但是會在這裏? } – Dante

+1

我看,你試着比較「JBArray [i] <最小」。你需要爲它實施oparation。 – Unick

回答

1

運算符超載<沒有定義。由於這是作業,我不會告訴你如何去做,但它應該和你已經定義的重載操作符非常相似。嘗試看看這個網站:http://www.learncpp.com/cpp-tutorial/96-overloading-the-comparison-operators/

此外,該聲明是不完全正確的。取而代之的

bool operator<(const smallest) 

應該

friend bool operator<(const JobBid &left, const JobBid &right); 

那麼對於定義:

bool operator<(const JobBid &left, const JobBid &right) 
{ 
    // for you to fill in 
} 

你錯過一個分號和參數的類型。

編輯:我還發現了另一個問題。你永遠不會定義JBArray [0],所以你的JBSmallest應該是JBArray[1],而不是JBArray[0]

+0

我超級困惑,它給了我一個錯誤,除非我提供了2個參數的定義。如果一個參數是數組而且是「最小」的int?看看你鏈接的網站,當他們定義相同的運算符時,他們引用該類中的變量。那麼我的數組列表和最小的int也應該在類中定義? – Dante

+0

@丹特我剛編輯過這篇文章。 – FCo

+0

在你的代碼中調用這個函數的方式就像''JBArray [i] FCo

0

你已經錯過的「布爾操作<(常量最小)」的定義

操作的實現可以是

friend bool JobBid::operator<(const JobBid& smallest) { 
    if(smallest.bid < this.bid && smallest.quote < this.quote) 
      return true; 
    else 
      return false; 
} 
+0

運營商的朋友聲明實際上取決於班級是否在通話的左側或右側。如果一個類將在表達式的右邊 - 並且不提供一個隱式轉換爲可以與左邊相比較的類型 - 您需要實現operator <作爲單獨的函數或作爲一個單獨的函數這個班的朋友。 –

1

你的方法聲明中缺少一個類型;你告訴它變量名稱和它的常量,但不是類型。試試這個:

bool operator<(const JobBid& jb) const 
{ 
    return (bid < jb.bid) && (quote < jb.quote); 
} 

我建議在方法的末尾添加const告訴編譯器和閱讀器,該方法不改變任何數據成員。

編輯1
的方法應該被添加到類:

class JobBid 
{ 
    private: 
     friend ostream& operator<<(ostream&, const JobBid&); 
     int bid; 
     int quote; 
    public: 
     JobBid& operator() (int, int); 
     bool operator<(const JobBid & jb) const 
     { 
      return (bid < jb.bid) && (quote < jb.quote); 
     } 
}; 

編輯2:實施類

class JobBid 
    { 
     private: 
      friend ostream& operator<<(ostream&, const JobBid&); 
      int bid; 
      int quote; 
     public: 
      JobBid& operator() (int, int); 
      bool operator<(const JobBid & jb) const; 
    }; 

bool JobBid::operator<(const JobBid & jb) const 
{ 
    return (bid < jb.bid) && (quote < jb.quote); 
} 

編輯3外:自由站立功能

 class JobBid 
     { 
      private: 
       friend ostream& operator<<(ostream&, const JobBid&); 
       int bid; 
       int quote; 
      public: 
       JobBid& operator() (int, int); 
       friend bool operator<(const JobBid & a, 
             const JobBid & b); 
     }; 

bool operator<(const JobBid & a, const JobBid & b) 
{ 
    return (a.bid < b.bid) && (a.quote < b.quote); 
} 
+0

返回這些錯誤: 26:34:錯誤:非成員函數'布爾運算符'(常量JobBid&)'不能有cv-限定符 26:34:錯誤:'布爾運算符<(常量JobBid&)'必須採取兩個論點 – Dante

+0

你是在課堂內外還是在外面宣佈的?在類中定義運算符使用1個參數。在類之外聲明該函數需要2個參數。 –

相關問題