新的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