【发布时间】:2016-08-16 06:06:13
【问题描述】:
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' ;
}
我知道代码可能很糟糕,但它是一个介绍类。无论哪种方式,我都试图在 main 的末尾返回我创建的数组列表的最小值。但是我发现尝试在我的类型“JobBid”上使用“
我在这里尝试这样做:
bool operator<(const smallest)
但我一定是做错了,在那之后我能够解决所有错误(我的意思是特定行上的错误),但现在我得到了这个:
undefined reference to `JobBid::operator<(JobBid)'
我不知道如何解决它。我认为在数组中找到最小对象的逻辑是正确的,所以它必须是带有小于号的东西。
【问题讨论】:
-
你对那个重载的操作符有定义吗?
-
我想我没有,我该如何定义它?我应该首先考虑最小的论点吗?我想我需要做这样的事情: JobBid& operator
-
我明白了,您尝试比较“JBArray[i]
-
由于缺少分号,不能成为您的minimal reproducible example。
-
此外,所有这些文本和八个变量
b1到q4看起来远非最小。