2016-04-22 28 views
0

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

我不知道如何解决它。我认为查找数组中最小对象的逻辑是正确的,所以它必须是小于号的东西。

+1

你有重载操作符的定义吗? – FCo

+0

我想我不会,我会如何定义它?我想首先考虑的是最小的参数吗?我想我需要做这样的事情: JobBid&operator <(){ //但是会在这里? } – Dante

+1

我看,你试着比较“JBArray [i] <最小”。你需要为它实施oparation。 – Unick

回答

1

运算符超载<没有定义。由于这是作业,我不会告诉你如何去做,但它应该和你已经定义的重载操作符非常相似。尝试看看这个网站: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]

+0

我超级困惑,它给了我一个错误,除非我提供了2个参数的定义。如果一个参数是数组而且是“最小”的int?看看你链接的网站,当他们定义相同的运算符时,他们引用该类中的变量。那么我的数组列表和最小的int也应该在类中定义? – Dante

+0

@丹特我刚编辑过这篇文章。 – FCo

+0

在你的代码中调用这个函数的方式就像''JBArray [i] FCo

0

你已经错过的“布尔操作<(常量最小)”的定义

操作的实现可以是

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

运营商的朋友声明实际上取决于班级是否在通话的左侧或右侧。如果一个类将在表达式的右边 - 并且不提供一个隐式转换为可以与左边相比较的类型 - 您需要实现operator <作为单独的函数或作为一个单独的函数这个班的朋友。 –

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

返回这些错误: 26:34:错误:非成员函数'布尔运算符'(常量JobBid&)'不能有cv-限定符 26:34:错误:'布尔运算符<(常量JobBid&)'必须采取两个论点 – Dante

+0

你是在课堂内外还是在外面宣布的?在类中定义运算符使用1个参数。在类之外声明该函数需要2个参数。 –