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:perator+( myClass& x) {
myClass z();
z.rows = rows+x.rows; z.cols = cols+x.cols;
return z; }
myClass& myClass: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:perator=(myClass)'"
"candidates are: myClass& myClass: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++.
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:perator+( myClass& x) {
myClass z();
z.rows = rows+x.rows; z.cols = cols+x.cols;
return z; }
myClass& myClass: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:perator=(myClass)'"
"candidates are: myClass& myClass: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++.