新的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)'
我不知道如何解決它。我認爲查找數組中最小對象的邏輯是正確的,所以它必須是小於號的東西。
你有重載操作符的定義嗎? – FCo
我想我不會,我會如何定義它?我想首先考慮的是最小的參數嗎?我想我需要做這樣的事情: JobBid&operator <(){ //但是會在這裏? } – Dante
我看,你試着比較「JBArray [i] <最小」。你需要爲它實施oparation。 – Unick