473,397 Members | 1,960 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Overloading problem in gnu but not in VC++?

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

myClass& myClass::operator=(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::operator=(myClass)'"
"candidates are: myClass& myClass::operator=(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++.

Dec 14 '06 #1
3 1234
* ki******@gmail.com:
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::operator+( 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::operator=(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::operator=(myClass)'"
"candidates are: myClass& myClass::operator=(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?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Dec 14 '06 #2

ki******@gmail.com wrote:
"no matching function for call to 'myClass::operator=(myClass)'"
"candidates are: myClass& myClass::operator=(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.

Dec 14 '06 #3
ki******@gmail.com wrote:
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::operator+( myClass& x) {
myClass z();
z.rows = rows+x.rows; z.cols = cols+x.cols;
return z; }

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

myClass& myClass::operator=(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

Dec 14 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Mohammad | last post by:
I'm implementing a c++ template class CScan to minipulate a series of numbers. I have implemented operator() to select a range of numbers it works fine for an expression like: scan1 = scan2(0,...
18
by: Joe Seigh | last post by:
Is there a good write on this. The textbooks I have fluff over on this? Specifically, I trying to dereference with 2 levels of type conversion not 1, i.e. X<T> -> z => Y<T> -> z => T* ->...
3
by: Robert Wierschke | last post by:
Hi I want to overload the operator<< for a class Vector. class Vector { double x; double y; double z;
1
by: Zachary Turner | last post by:
I'm having trouble overloading the global operator new and delete. I want to do this for debugging purposes to trace allocations and deallocations. What I have is #ifdef _DEBUG //...
6
by: c++newbie | last post by:
Hi all, I try to compile the following classes: main.cpp: #include <algorithm> #include <iostream> #include <fstream> #include <iterator>
5
by: Banshee | last post by:
Hi there, I tried with Visual C++ 6.0 to overloading the ostream operator for an object. While compiling I receive this error: Compiling... main.cpp C:\\main.cpp(8) : error C2678: binary...
51
by: Jojo | last post by:
Is there any way to get to the left-hand side of an operator? Consider the following (this is not meant to be perfect code, just an example of the problem): class Matrix { public: int data;...
6
by: Matthew Cook | last post by:
I would like to overload the unary minus operator so that I can negate an instance of a class and pass that instance to a function without creating an explicit temporary variable. Here is an...
2
by: Colonel | last post by:
It seems that the problems have something to do with the overloading of istream operator ">>", but I just can't find the exact problem. // the declaration friend std::istream &...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.