【问题标题】:C++: comparing operand types of the same typeC++:比较相同类型的操作数类型
【发布时间】:2015-04-23 19:14:08
【问题描述】:

我只使用了大约一周的 C++ 自学。我尝试搜索类似的问题,但找不到。但是,这可能是由于不知道用于我的问题的适当搜索词。

我定义了以下结构:

struct Sales_data {                                                             
  // empty bracket initializes to empty string                                  
  std:: string bookNo{};                                                        
  unsigned units_sold = 0;                                                      
  double revenue = 0.0;                                                         
};

在我的int main() {} 中,我使用if 循环来检查book 是否为非空,而Sales_data books; 之前已声明。也就是说,我有

#include <iostream>
#include <string>

struct Sales_data {                                                             
  // empty bracket initializes to empty string                                  
  std:: string bookNo{};                                                        
  unsigned units_sold = 0;                                                      
  double revenue = 0.0;                                                         
};

int main(){
  Sales_data books;
  double price = 0.0;
  // some ostream code here
  for (int i = 0; i >= 0; ++i) {                                                
    // The for loop keeps track and counts the books                            
    while (std::cin >> books.bookNo >> books.units_sold >> price) {             
      /*                                                                        
       * while loop to allow the user to input as many books as they would      
       * like                                                                   
       */                                                                       
      if (books != Sales_data()) {                                              
        // if books is not empty print which number book for i                  
        i += 1;                                                                 
        std::cout << "Book " << i << " is " << books << std::endl;              
      }                                                                         
    }                                                                           
  }            
  return 0;
}

问题出在if (books != Sales_data()) ...,这是错误所在

error: no match for ‘operator!=’ (operand types are
‘Sales_data’ and ‘Sales_data’)
       if (books != Sales_data()) {

它说操作数类型具有相同的类型,所以我不太明白问题出在哪里。

【问题讨论】:

  • ` // 空括号初始化为空字符串 ` 不正确。不需要它们
  • @EdHeal 所以就留空吧?
  • std:: string bookNo 可以

标签: c++ types operators


【解决方案1】:

您需要为您的结构实现operator!=。

struct Sales_data
{
   ...

  bool operator!=(const Sales_data& other)
  {
     // Logic to determine if sales data are not equal
     return ...;
  }
}; // end definition of struct Sales_data

或者这可以实现为一个独立的操作符

bool operator!=(const Sales_data& data1, const Sales_data& data2)
{
   // Logic to determine if the two instances are not equal
   return ...;
}

【讨论】:

  • 如果可能的话,首选独立的操作员。
  • @dustin bool 是算子函数的返回值。您应该尝试查找 C++ 语法的教程。编辑:啊,误读了你的问题。在第二个选项中,operator!= 是在类或结构之外声明的函数,与 main 相同。
  • @dustin 第二个选项是一个独立的函数,声明在 main 和 struct 之外。
  • @dustin 您需要编写逻辑来比较两个结构中的各个字段并将其转换为表示它们是否相等的布尔值。例如,return data1.units_sold != data2.units_sold &amp;&amp; data1.revenue != data2.revenue &amp;&amp; // any other comparisons necessary
  • 谢谢。在您发表评论之前,我能够弄清楚这一点。你知道吗:cannot bind ‘std::ostream {aka std::basic_ostream&lt;char&gt;}’ lvalue to ‘std::basic_ostream&lt;char&gt;&amp;&amp;’ std::cout &lt;&lt; books &lt;&lt; std::endl;我读了很多关于这个的帖子,但我不明白如何平息它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-03-18
  • 2012-09-14
  • 1970-01-01
  • 2019-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多