C++ operator overloading error

c++, c++ operator overloading, error, programming, solved

Thread Tools Search this Thread
# 1  
Old 12-06-2018
C++ operator overloading error

I defined my own String class and overloaded the output operator with
friend ostream& operator<<(ostream& os, const myString& str);
//overloads the << operator so it can be used to output values of type myString

which works fine. Until I try to execute the following statement:
// + concatenation operator is already defined for myString
cout << (str4=str2+str) << endl; //This statement concatenates str to str2, then assigns it to str4 and returns str4, right?

And, I get error
error: no match for ‘operator<<’ (operand types are ‘std::ostream’ {aka ‘std::basic_ostream<char>’} and ‘void’)
  cout << (str4=str2+str) << endl;

The above code works fine when I change the concatenation statement as follows:
cout << str4 << endl;

What did I miss?
# 2  
Old 12-06-2018
What type does your 'operator =' return?
# 3  
Old 12-06-2018
Originally Posted by Corona688
What type does your 'operator =' return?
void operator=(const myString& rside);

# 4  
Old 12-06-2018
That means the expression 'str2=whatever' returns void -- or rather, returns nothing. So there's nothing to cout.

Instead have it return const myString & and at the end, put return(*this);
# 5  
Old 12-06-2018
I changed the member function like this:
myString operator=(const myString& rside)

and return rside at the end.
# 6  
Old 12-06-2018
Returning rside is clever and ought to work, but return-by-value is a bad idea -- that makes a local copy, which isn't just wasteful, in some circumstances that's an infinite recursion and out-of-memory crash. Assignment operators shouldn't use copy constructors.

You pretty much have to return a reference here.

const myString &operator=(const myString& rside)

This User Gave Thanks to Corona688 For This Post:
milhan (12-06-2018)
# 7  
Old 12-06-2018
makes sense. Thanks for the help.
This User Gave Thanks to milhan For This Post:
Corona688 (12-07-2018)

Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Getting unknown operator error while using a function Samah Shell Programming and Scripting 8 04-19-2017 05:25 PM
Binary operator expected Error mohtashims Shell Programming and Scripting 1 01-20-2017 04:10 PM
Getting syntax error with awk ternary operator Michael Stora Shell Programming and Scripting 4 04-24-2015 07:13 PM
Help - binary operator expected error pchang Shell Programming and Scripting 2 04-06-2015 12:51 PM
mysqld overloading cpu of VPS rizwan65 UNIX for Dummies Questions & Answers 2 06-08-2011 06:30 AM
c++ assignment operator overloading bashuser2 Programming 1 12-18-2010 10:19 PM
[: =: unary operator expected error mbf123 UNIX for Dummies Questions & Answers 24 12-12-2010 11:18 AM
Delete operator overloading with multiple arguments. kapilkumawat Programming 1 01-15-2010 08:48 PM
Error : -ne: unary operator expected ritu.s UNIX for Dummies Questions & Answers 2 12-04-2009 08:37 AM
File already exists error while using '>' operator jathin12 UNIX for Dummies Questions & Answers 5 02-26-2009 08:05 AM
C++ Optr Overloading Nagapandi Programming 1 02-26-2009 05:53 AM
syntax error: `-a' unexpected operator/operand in IF dba.admin2008 Shell Programming and Scripting 4 11-14-2008 05:16 PM
unary operator expected error ssuresh1999 UNIX for Dummies Questions & Answers 2 10-28-2008 10:33 PM
xlC compilation error when dealing with operator overloading luop0812 AIX 1 04-09-2008 05:09 PM
binary operator expected error apps_user Shell Programming and Scripting 7 05-11-2007 08:33 PM