dustin
dustin

Reputation: 4406

C++: comparing operand types of the same type

I have only been using C++ for about a week teaching myself. I tried searching for a similar question but I couldn't find one. However, this could be due to not knowing the appropriate search terms to use for my problem.

I have defined the following structure:

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

In my int main() {}, I using a if loop to check if book is nonempty where Sales_data books; has been declared earlier. That is, I have

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

The problem is at if (books != Sales_data()) ... which is where the error is

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

It says the operand types have the same type so I don't quite get what the problem is.

Upvotes: 0

Views: 185

Answers (1)

lcs
lcs

Reputation: 4245

You need to implement operator!= for your struct.

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

or this can be implemented as a freestanding operator

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

Upvotes: 3

Related Questions