472,794 Members | 2,221 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Operator Overloading Problems

Hi,

I'm writing a sparse matrix class for class and I cannot seem to get
operator overloading to work properly.

I've overloaded an operator with the code here.

matrix operator +(matrix one, matrix other) {
cout << "adding" << endl;
return matrix(NULL,2,2); //doesn't actually carry out the operation,
but returns a different value, i'm just trying to test if it works
}

And i'm calling it with such a method:

matrix res(NULL,1,1);
res = *matrix1 + *matrix2; //this doesn't work
(*matrix1 + *matrix2).display() //this does? //it displays what i'm
looking for
res.display() //this displays the original value of res

With matrix1 and matrix2 of course being pointers to matrix objects.
Now, however after the call to the overloaded operator, res has the
same value, but when I call (*matrix1 + *matrix2) it displays the
added value i'm looking for. I can't quite understand for the life of
me why this value doesn't get assigned to the res variable.

Thanks for your time.

P.S. What's with all the spam on this newsgroup?

Nov 18 '07 #1
1 1579
On 2007-11-18 15:39:38 -0500, xkenneth <xk******@gmail.comsaid:
Hi,

I'm writing a sparse matrix class for class and I cannot seem to get
operator overloading to work properly.

I've overloaded an operator with the code here.

matrix operator +(matrix one, matrix other) {
cout << "adding" << endl;
return matrix(NULL,2,2); //doesn't actually carry out the operation,
but returns a different value, i'm just trying to test if it works
}

And i'm calling it with such a method:

matrix res(NULL,1,1);
res = *matrix1 + *matrix2; //this doesn't work
(*matrix1 + *matrix2).display() //this does? //it displays what i'm
looking for
res.display() //this displays the original value of res

With matrix1 and matrix2 of course being pointers to matrix objects.
Now, however after the call to the overloaded operator, res has the
same value, but when I call (*matrix1 + *matrix2) it displays the
added value i'm looking for.
This suggest the problem maybe with the assignment operator. Did you
actually defined one? Can we see it?
I can't quite understand for the life of
me why this value doesn't get assigned to the res variable.

Thanks for your time.

P.S. What's with all the spam on this newsgroup?
The best way to fight spam is just to ignore it. Don't talk about it,
don't ask about it. Just pretend they're not here at the first place.
So, it seems like I just broke my own rule here, but you don't have to
make it worse by commenting back on this point. Just ignore it.

--

-kira

Nov 18 '07 #2

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,...
4
by: hall | last post by:
Hi all. I have run into a problem of overloading a templatized operator>> by a specialized version of it. In short (complete code below), I have written a stream class, STR, which defines a...
7
by: Eckhard Lehmann | last post by:
Hi, I try to recall some C++ currently. Therefore I read the "Standard C++ Bible" by C. Walnum, A. Stevens and - of course there are chapters about operator overloading. Now I have a class...
19
by: jacob navia | last post by:
C++ introduced an interesting feature (among others): operator overloading. The idea is to build a mechanism for the user defining its own number types and the operations to be done with them. ...
17
by: Chris | last post by:
To me, this seems rather redundant. The compiler requires that if you overload the == operator, you must also overload the != operator. All I do for the != operator is something like this: ...
6
by: TuxC0d3 | last post by:
Hi! I'm diving into the some more ++ specific aspects of c++ (and finally accepting that c++ is more than "a plus added to c" :), so that means using namespaces, templates, std::strings, lists,...
67
by: carlos | last post by:
Curious: Why wasnt a primitive exponentiation operator not added to C99? And, are there requests to do so in the next std revision? Justification for doing so: C and C++ are increasingly used...
3
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj,...
11
by: jakester | last post by:
I am using Visual C++ 2007 to build the code below. I keep getting linkage error. Could someone please tell me what I am doing wrong? The code works until I start using namespace for my objects. ...
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.