获取函数运算符<未定义的引用
Getting an undefined reference to for an operator of < function
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)'
而且我不确定如何解决它。我认为在数组中查找最小对象的逻辑是正确的,因此它必须是带有小于号的东西。
你的方法声明缺少一个类型;你告诉它变量名和它的常量,而不是类型。 试试这个:
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);
}
您错过了"bool operator<(const smallest)"的定义
运算符的实现可以是
friend bool JobBid::operator<(const JobBid& smallest) {
if(smallest.bid < this.bid && smallest.quote < this.quote)
return true;
else
return false;
}
重载<
运算符没有定义。由于这是家庭作业,我不会确切地告诉您如何做,但它应该与您已经定义的重载运算符非常相似。尝试查看此网站: 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]
。
- 对C宏的未定义引用,但在定义它时会出现重新定义错误
- 编译时的 CImg 库返回对"__imp_SetDIBitsToDevice"的未定义引用
- 对Py_Initialize()的未定义引用
- 使用mysql c++连接器的未定义引用
- 对 Scalar ::Scalar() 的未定义引用
- 对复制 CTOR 和 CTOR 的未定义引用
- 对显式实例化的模板函数的未定义引用
- TensorRT (C++ API) 对"createNvOnnxParser_INTERNAL"的未定义引用
- 2个模板化类的非模板友元函数未定义引用错误
- 编译 libfluid 样本控制器时对"event_base_del_virtual"的未定义引用
- 获取对function_name的未定义引用
- 对 'std::thread::_M_start_thread CMake 的未定义引用进行基准测试
- 对结构方法的未定义引用
- 使用内联函数 c++ 的未定义引用
- 对 CMake 中'cudaRegisterLinkedBinary'链接错误的未定义引用?
- 对 DLOPEN 的未定义引用
- QT C++中对全局变量的未定义引用
- 快速数学导致对"__pow_finite"的未定义引用
- 对 boost::system::d etail::system_category_instance 的未定义引用,从
- OpenCV 3.4.3 中对 'cv::String::d eallocate()' 错误的未定义引用