【问题标题】:Getting an undefined reference to for an operator of < function为 < 函数的运算符获取未定义的引用
【发布时间】: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 看起来远非最小。

标签: c++ boolean operators


【解决方案1】:

您的方法声明缺少类型;你告诉它变量名和它的常量,而不是类型。试试这个:

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

【讨论】:

  • 返回以下错误:26:34:错误:非成员函数 'bool operator
  • 你在哪里声明的,在课堂内还是在课堂外?在类中定义运算符使用 1 个参数。在类外声明函数需要2个参数。
【解决方案2】:

重载的&lt; 运算符没有定义。由于这是家庭作业,我不会告诉你具体怎么做,但它应该与你已经定义的重载运算符非常相似。试试看这个网站: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]。

【讨论】:

  • 我非常困惑,除非我为定义提供 2 个参数,否则它会给我一个错误。一个参数应该是数组,一个是“最小”的整数吗?查看您链接的站点,当他们定义相同的运算符时,他们引用了类中的变量。那么我的数组列表和最小的int也应该在类中定义吗?
  • 在你的代码中调用这个函数的方式就是JBArray[i] &lt; smallest,就像你有它一样,因为两者都是JobBid类型。
【解决方案3】:

你错过了“bool operator

操作符的实现可以是

friend bool JobBid::operator<(const JobBid& smallest) {
     if(smallest.bid < this.bid && smallest.quote < this.quote)
           return true;
     else
           return false;
}

【讨论】:

  • 运算符的友元声明实际上取决于一个类是在调用的左侧还是右侧。如果一个类将位于表达式的右侧 - 并且不提供到可以与左侧比较的类型的隐式转换 - 您需要将 operator
猜你喜欢
  • 2012-06-24
  • 1970-01-01
  • 2018-09-17
  • 1970-01-01
  • 2020-09-15
  • 2016-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多