Olejniczka
Olejniczka

Reputation: 13

C++ compiler error, erasing letter from word

Compiler shows this error:

error: invalid operands to binary expression ('basic_ostream<char, std::__1::char_traits<char> >' and 'void')

What am I doing wrong?

#include <iostream>
#include <string>
using namespace std;

void remove(string x, string y)
{
   while (x.find_first_of(y) != -1)
   {
      x.erase(x.find_first_of(y), 1);
   }
}

int main()
{
   string a, b;
   cout << "Enter word: ";
   cin >> a;
   cout << "Sign: ";
   cin >> b;
   cout << "Result: " << remove(a, b) << endl;

   return 0;
}

Upvotes: 1

Views: 44

Answers (2)

Zarif
Zarif

Reputation: 485

The function doesn't return anything (that is what is meant by void) . So you can't output anything

Write

string remove(string x,string y){
..............
return x;
}

Upvotes: 1

Mohammed Deifallah
Mohammed Deifallah

Reputation: 1330

remove() function is void, which returns nothing. However, cout doesn't overload the operator << for void datatype.

Upvotes: 0

Related Questions