This is a discussion on operator= problems within the Coding in General forums, part of the Development/Scripting category; Here's an example of the error. If you try to compile this with the latest version of gcc it will ...
|
|||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
|
|||
|
Here's an example of the error. If you try to compile this with the latest version of gcc it will fail. For some reason gcc seems unable to pass the returned testCase type from Q * x, the operator* function, to the operator= function. Is there a way to get it to do this? The error was brought up in a much larger program I'm working on where an object's function returns an object of the same type, and then assigns it to a different object with operator=. I've tried converting the operator* function to a friend function instead of a member function, but that gives exactly the same errors.
Code:
#include<iostream>
using namespace std;
class testClass
{
private:
int a;
public:
testClass(testClass& input) { a = input.a; }
testClass(int x) { a = x; }
testClass() { a = 1; }
testClass operator=(testClass& input)
{ a = input.a; }
testClass operator*(int x)
{ testClass temp(a * x); return temp; }
};
int main()
{
int x = 3;
int y = 4;
testClass R(y);
testClass Q;
R = Q * x;
return 0;
}
|
| Sponsored Links | ||
|
|
|
|||
|
Don't know if you still need help with this, but... I just joined the forum and it seems like a ghost town.
The problem is with your code and not gcc. Your operator= method requires the argument to be a modifiable reference to a testClass (also sometimes called an l-value meaning it is allowed to appear on the left-hand side of an assignment). The return type of your operator* method is an r-value (meaning it is only allowed to appear on the right-hand side of an assignment). It is good to become familiar with the proper signature and return-type of assignment statements. When dealing with objects, an assignment should declare its formal parameter to be a *constant* reference to an object (this allows the compiler to allow temporaries or other r-values as argument . Also, an assignment typically returns a (modifiable) reference to an object, rather than a object value. This allows us to write statements likeCode:
x = y = 2; Code:
testClass& operator= (testClass const& input)
{
a = input.a;
return *this;
}
Hope this helps. -Matthew- |
|
|||
|
I'm not currently working on the code anymore, but thanks for your suggestion. The error I was running into was occuring when I was using an operator= function like you suggest, albiet without the constant parameter. I had created a simple test case to check and see if I could find the basic error in my code, but kept getting the same problem. The code I posted was the most simplistic code I could create, obviously too simplistic since I forgot to include the return statement in my operator= function. Unfortunately I didn't catch that simple error because even when that was corrected the compilation process halted on the same error. I ended up with a work around that was not pretty but allowed me to handle data from the same source in two unrelated scopes. Thanks for the input though.
|
![]() |
| Bookmarks |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) |
|
| Thread Tools | |
| Display Modes | |
|
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Internet problems | norm134c | Getting started tutorials | 2 | 03-25-2008 12:20 AM |
| Help with MINIX problems | grndpilot99 | Linux software | 0 | 10-03-2007 08:29 AM |
| PHPMyAdmin Problems | JoeDively | Coding in General | 0 | 09-21-2007 09:30 PM |
| nfs problems | marros | Linux software | 2 | 10-21-2006 01:30 AM |
| .htaccess problems | rcordeiro | Linux software | 4 | 07-05-2006 07:32 PM |