Overloading problem in gnu but not in VC++?

K

kim.ruhl

I've been coding in visual C++ but am migrating to linux + gnu. I'm
not an expert, but my programs seemed to work fine. When I take all
the junk out of the VC++ codes and try to run them on my linux machine,
the compiler balks about my operator overloads. Here is a simple
example:

class myClass{
private:
int rows;
int cols;
public:
int myClass();
int myClass(int, int);
myClass operator+(myClass&);
myClass& operator=(myClass&); };

in myClass.cpp is the constructor code plus

myClass myClass::eek:perator+( myClass& x) {
myClass z();
z.rows = rows+x.rows; z.cols = cols+x.cols;
return z; }

myClass& myClass::eek:perator=(myClass& rhs){
rows=rhs.rows; cols=rhs.cols;
return *this; }

Trying to compile this

#include <iostream>
#include "myClass.h"

int main(){
myClass x(1,2);
myClass y(1,2);
myClass z();

z.x+y;

return 0;}

I get the message:

"no matching function for call to 'myClass::eek:perator=(myClass)'"
"candidates are: myClass& myClass::eek:perator=(myClass&)"

I'm mystified by this. The operator+ returns an object (by value) of
type myClass, but can't the operator= take it by reference? This code
runs fine in VC++ which worries me a little. Any ideas as to what is
going on? I care more about why this doesn't work in gnu than why it
does work in VC++.
 
A

Alf P. Steinbach

* (e-mail address removed):
I've been coding in visual C++ but am migrating to linux + gnu. I'm
not an expert, but my programs seemed to work fine. When I take all
the junk out of the VC++ codes and try to run them on my linux machine,
the compiler balks about my operator overloads. Here is a simple
example:

class myClass{
private:
int rows;
int cols;
public:
int myClass();
int myClass(int, int);
myClass operator+(myClass&);
myClass& operator=(myClass&); };

Why are you requiring the right hand side of assignment to an lvalue?

in myClass.cpp is the constructor code plus

myClass myClass::eek:perator+( myClass& x) {
myClass z();

Why are you declaring a function z?

z.rows = rows+x.rows; z.cols = cols+x.cols;
return z; }

myClass& myClass::eek:perator=(myClass& rhs){
rows=rhs.rows; cols=rhs.cols;
return *this; }

Trying to compile this

#include <iostream>
#include "myClass.h"

int main(){
myClass x(1,2);
myClass y(1,2);
myClass z();

z.x+y;

return 0;}

I get the message:

"no matching function for call to 'myClass::eek:perator=(myClass)'"
"candidates are: myClass& myClass::eek:perator=(myClass&)"

I'm mystified by this. The operator+ returns an object (by value) of
type myClass, but can't the operator= take it by reference? This code
runs fine in VC++ which worries me a little. Any ideas as to what is
going on? I care more about why this doesn't work in gnu than why it
does work in VC++.

Are you sure the above is the code you compiled?
 
N

Noah Roberts

"no matching function for call to 'myClass::eek:perator=(myClass)'"
"candidates are: myClass& myClass::eek:perator=(myClass&)"

I'm mystified by this. The operator+ returns an object (by value) of
type myClass, but can't the operator= take it by reference?

No. A temporary can only be passed as a const reference or by value.
Adjust your = operator for const correctness.
 
M

mlimber

I've been coding in visual C++ but am migrating to linux + gnu. I'm
not an expert, but my programs seemed to work fine. When I take all
the junk out of the VC++ codes and try to run them on my linux machine,
the compiler balks about my operator overloads. Here is a simple
example:

class myClass{
private:
int rows;
int cols;
public:
int myClass();
int myClass(int, int);
myClass operator+(myClass&);
myClass& operator=(myClass&); };

in myClass.cpp is the constructor code plus

myClass myClass::eek:perator+( myClass& x) {
myClass z();
z.rows = rows+x.rows; z.cols = cols+x.cols;
return z; }

myClass& myClass::eek:perator=(myClass& rhs){
rows=rhs.rows; cols=rhs.cols;
return *this; }

Trying to compile this

#include <iostream>
#include "myClass.h"

int main(){
myClass x(1,2);
myClass y(1,2);
myClass z();

z.x+y;

return 0;}

I get the message:

"no matching function for call to 'myClass::eek:perator=(myClass)'"
"candidates are: myClass& myClass::eek:perator=(myClass&)"

I'm mystified by this. The operator+ returns an object (by value) of
type myClass, but can't the operator= take it by reference? This code
runs fine in VC++ which worries me a little. Any ideas as to what is
going on? I care more about why this doesn't work in gnu than why it
does work in VC++.

Your code obviously doesn't compile anywhere. See the guidelines for
posting code that doesn't work:

http://parashift.com/c++-faq-lite/how-to-post.html#faq-5.8

Try this code:

class myClass{
private:
int rows;
int cols;
public:
myClass(int r=0, int c=0) : rows(r), cols(c) {}
myClass operator+(myClass&);
myClass& operator=(const myClass&); };

myClass myClass::eek:perator+( myClass& x) {
myClass z;
z.rows = rows+x.rows; z.cols = cols+x.cols;
return z; }

myClass& myClass::eek:perator=(const myClass& rhs){
rows=rhs.rows; cols=rhs.cols;
return *this; }


int main(){
myClass x(1,2);
myClass y(1,2);
myClass z;

z=x+y;

return 0;}

Cheers! --M
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,151
Latest member
JaclynMarl
Top