C++ operator overloading error

Login or Register to Reply

Thread Tools Search this Thread
# 1  
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?
# 4  
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);
# 6  
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:
Login or Register to Reply

Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
mysqld overloading cpu of VPS
Hi bros I have a VPS 512mb (Burst 2GB) with Kloxo installed and hosting few sites on it with not much traffic I am facing high cpu load for the last few days and seems mysqld is overloading the cpu Any suggestion will be appreciated Regards Rizwan Top output is as under top -...... UNIX for Dummies Questions & Answers
UNIX for Dummies Questions & Answers
c++ assignment operator overloading
Hello everyone! Suppose that I have something like this A a; a.mem=new int; A b = a; where class A { public: int * mem; A() : mem(NULL) { } ~A() { if (mem!=NULL) delete mem;... Programming
Delete operator overloading with multiple arguments.
Hi, I have an requirement to overload the delete operator in C++, but it should also accept the sizeof() the object that is to be deleted. Actually I am trying to built a custom memory allocator and deallocator like a pool, which makes me to overload the delete operator. Small example of the...... Programming
C++ Optr Overloading
Hi All, In C++ one operator can be overloaded, but it can't be overloaded by it's own derieved class Please let me know operator. Thanks, Naga:cool:... Programming
xlC compilation error when dealing with operator overloading
Hi, I have a piece of C++ code that can be compiled using g++, but reports an error when compiled with xlC: xlC -DHAVE_CONFIG_H -I../SRC -I../include -DNoChange -DSPRNG_MPI -q64 -DLONG64=long -I/usr/lpp/ppe.poe/include -DLONG64=long -c -o libsprng_a-bignum.o bignum.cpp "bignum.cpp",...... AIX