nixCraft Linux Forum

nixCraft

Linux Tech Support Forum

operator= problems

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 ...


Go Back   nixCraft Linux Forum > Development/Scripting > Coding in General

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 07-08-2006, 12:27 AM
Junior Member
 
Join Date: Jul 2006
Posts: 3
Rep Power: 0
mtalexan
Default operator= problems

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;
}
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 10-16-2006, 11:10 PM
Junior Member
 
Join Date: Oct 2006
Location: Hudson Valley, NY
Posts: 2
Rep Power: 0
Q.E.D.
Default

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 like
Code:
x = y = 2;
So, to summarize, the declaration of your assignment operator should look like
Code:
      testClass& operator= (testClass const& input)
         {
            a = input.a;
            return *this;
         }
If you are not familiar with the special pointer 'this', then I recommend that you look it up.

Hope this helps.

-Matthew-
Reply With Quote
  #3 (permalink)  
Old 10-16-2006, 11:33 PM
Junior Member
 
Join Date: Jul 2006
Posts: 3
Rep Power: 0
mtalexan
Default

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.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads

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


All times are GMT +5.5. The time now is 05:18 AM.


Powered by vBulletin® Version 3.7.2 - Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36