1

i'm kinda new to coding, need to do a program for some kind of a database. i saw a tutorial on youtube of some dude, copied his code but i get an error and i don't know how to fix it.

the error : no operator "==" matches these operands ; refering to the if ( outs == cout )

here's the code :

void Employee::output(ostream& outs)
{
    if (outs == cout)
    {
        outs << "Name: " << name << endl;
        outs << "ID number: " << id_number << endl;
        outs << "Address: " << address << endl;
        outs << "Salary: " << salary << endl;
        outs << "Years worked at company: " << year_started << endl;
    }
    else {
        outs << name << endl;
        outs << id_number << endl;
        outs << address << endl;
        outs << salary << endl;
        outs << year_started << endl;
    }
}

here's how i declared output :

void output(std::ostream& outs);

added the iostream and string with #include < >

3 Answers3

5

Stream objects are not comparable, but you can compare their addresses:

    if ( & outs == & cout )
  • it worked after adding & – Miciu P'teito Apr 10 '17 at 19:57
  • Anyone has any insight on the down side of this? – Tony J Apr 10 '17 at 20:12
  • @TonyJ `(outs == cout) == true` (if works) does not necessarily imply that they must be the same object... ? – felix Apr 10 '17 at 20:29
  • @felix: That doesn't compile. TonyJ Shouldn't be any downsides, the OP was checking if two streams are "equal" to each other, which doesn't make sense. This is checking if two names are the same stream object, which is exactly what the OP wants. – Mooing Duck Apr 10 '17 at 20:55
  • @MooingDuck `outs.rdbuf() == cout.rdbuf()` may be considered as a better alternative. – felix Apr 10 '17 at 21:35
  • @felix Why would that be? –  Apr 10 '17 at 21:36
  • @NeilButterworth Take a look at this [answer](http://stackoverflow.com/questions/3318714/check-if-ostream-object-is-cout-or-ofstream-c/5293107#5293107) – felix Apr 10 '17 at 21:50
1

You can't compare streams for equality.

Instead, you can implement different functions, one for cout and one for a fstream.

I suggest two different functions, noting that one has a prompt and the other doesn't.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
1

You can use a completely different approach to create debug output that does not depend on comparing whether the stream being written to is std::cout. This approach relies on whether a flag has been turned on or not.

Create a helper namespace

The helper namespace can hold the data and provide convenience functions to manipulate the data.

namespace Employee_NS
{
   bool writeVerbose = false;

   template <bool val>
   std::ostream& verbose(std::ostream& outs)
   {
      writeVerbose = val;
      return outs;
   }

   std::ostream& operator<<(std::ostream& outs, std::ostream& (*fun)(std::ostream&))
   {
      return fun(outs);
   }
};

Implement Employee::output differently

Change the return type from void to std::ostream& and make the function a const member function.

std::ostream& Employee::output(std::ostream& outs) const
{
   if ( Employee_NS::writeVerbose )
   {
      outs << "Name: " << name << std::endl;
      outs << "ID number: " << id_number << std::endl;
      outs << "Address: " << address << std::endl;
      outs << "Salary: " << salary << std::endl;
      outs << "Years worked at company: " << year_started << std::endl;
   }
   else {
      outs << name << std::endl;
      outs << id_number << std::endl;
      outs << address << std::endl;
      outs << salary << std::endl;
      outs << year_started << std::endl;
   }

   return outs;
}

Add a suitable operator<< function

Add a non-member function to be able use Employee objects in an intuitive manner.

std::ostream& operator<<(std::ostream& out, Employee const& e)
{
   return e.output(out);
}

Now, you can create verbose output regardless of whether the output goes into a file or to std::cout.

Employee e;

// Verbose output to cout
std::cout << Employee_NS::verbose<true> << e; 

std::ofstream out("test.txt");

// Verbose output to the file
out << Employee_NS::verbose<true> << e; 

// Non-verbose output to the file
out << Employee_NS::verbose<false> << e; 

This approach elevates the decision of whether to create verbose output to the calling function. It also provides the ability to create verbose output in any output destination.

R Sahu
  • 204,454
  • 14
  • 159
  • 270