c++ - 为 < 函数的运算符获取 undefined reference

标签 c++ boolean operators

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);
}

关于c++ - 为 < 函数的运算符获取 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36796699/

相关文章:

rust - 我应该在新类型上实现 AddAssign 吗?

c++ - 如何在我的vs2010项目中使用wxformbuilder生成的c++文件

c++ - 复制构造函数快捷方式

c++ - 使用 find 在 unordered_set 中查找多个值

Powershell: boolean 函数的命名约定

ios - Objective-C中的bool、Boolean、BOOL有区别吗?

c# boolean 序列化问题

javascript - 为什么 ~-1 等于 0 而 ~1 等于 -2?

c++ - && 等逻辑运算符操作数的求值顺序

c++ - 为什么 is_invocable 与引用参数的工作方式不同?