error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'void' (or there is no acceptable conversion)

StackOverflow https://stackoverflow.com/questions/23231165

  •  07-07-2023
  •  | 
  •  

Question

I'm not sure what the problem is. It gives me the error:

error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'void' (or there is no acceptable conversion)

I overloaded the << so it shouldn't be giving me this error, right?

#ifndef ANIMAL_H_
#define ANIMAL_H_ 
#include <iostream> 
#include <string>
using namespace std;

static int counter;
static int getAnimalCount() { return counter; }

class Animal {
    protected:
       string *animalType;
    public:
       virtual void talk() = 0;
       virtual void move() = 0;
       string getAnimalType() { return *animalType; }

               //PROBLEM RIGHT HERE V

       friend ostream& operator<<(ostream&out, Animal& animal) {
           return out << animal.getAnimalType() << animal.talk() << ", " << animal.move();
       };
       ~Animal() {
           counter--;
           animalType = NULL;
       }
 };
 class Reptile : public Animal { 
     public:
         Reptile() { animalType = new string("reptile"); };
 };
 class Bird : public Animal {
     public:
         Bird() { animalType = new string("bird"); };
 };
 class Mammal : public Animal{
     public:
         Mammal() { animalType = new string("mammal"); };
 };
 #endif /* ANIMAL_H_ */
Was it helpful?

Solution

virtual void talk() = 0; specifies a function with return type void. This means it does not return anything. The same happens when you define Animal::move as virtual void move() = 0;.

out << animal.getAnimalType() << animal.talk() << ", " << animal.move(); tries to print the result of animal.talk() and the result of animal.move() - neither of which exists (remember, neither talk() nor move() return any value!)

OTHER TIPS

Your overloaded << operator looks like this:

return out << animal.getAnimalType() << animal.talk() << ", " << animal.move();

First of all, the talk() and move() methods return void. You cannot print void.

Point is at code generator. I have class member Dot.printX(), and in output stream I used cout<<Dot.printX<<endl; so that cause an error.

Adding printX() will fix a problem!

Good luck!

same this happens for this program too

#include <iostream>

void UNH(double b)
{
    std::cout << UNH(b) << std::endl;

}
int main()
{
    UNH(5); 
    return 0;
}

the thing is void cant take printing value at all. Also for conversion rules, it doesn't work,

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top