Thread: operator overloading not working

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    71

    operator overloading not working

    I am new to C++ plz help

    The below code does not work.. (printing garbage value )can any 1 explain y....

    Code:
    #include <iostream>
    #include <string.h>
    using namespace std;
    
    struct bignum
    {
    	char digits[1000];
    	int lastdigit;
    	
    	bignum operator = (char *str)
    	{	
    		int j=0;
    		bignum temp;
    		temp.lastdigit=strlen(str)-1;
    		
    //		printf("len=%d\n",temp.lastdigit);	/* working fine over here */
    		
    		for(int i=temp.lastdigit;i>=0;i--)
    			temp.digits[i]=str[j++];
    		
    		return temp;
    	}
    }; 
    	
    	
    int main()
    {
    	bignum num;
    	char s[10]="12345";
    	
    	num=s;
    	printf("%d\n",num.lastdigit);  /* why printing garbage value ?? */ 
    	
    return 0;
    }
    And also when operator overload is declared outside the struct i get a compile error saying
    Code:
    addition.cpp:12: error: ‘bignum operator=(char*)’ must be a nonstatic member function
    addition.cpp: In function ‘int main()’:
    addition.cpp:32: error: no match for ‘operator=’ in ‘num = s’
    addition.cpp:7: note: candidates are: bignum& bignum::operator=(const bignum&)
    Following is the code that gives compile error

    Code:
    #include <iostream>
    #include <algorithm>
    #include <string.h>
    using namespace std;
    
    struct bignum
    {
    	char digits[1000];
    	int lastdigit;
    }; 
    
    bignum operator = (char *str)
    {	
    	int j=0;
    	bignum temp;
    	temp.lastdigit=strlen(str)-1;
    		
    //	printf("len=%d\n",temp.lastdigit);	/* working fine over here */
    		
    	for(int i=temp.lastdigit;i>=0;i--)
    		temp.digits[i]=str[j++];
    		
    	return temp;
    }
    
    	
    int main()
    {
    	bignum num;
    	char s[10]="12345";
    	
    	num=s;
    	printf("%d\n",num.lastdigit);  /* why printing garbage value ?? */ 
    	
    return 0;
    }
    Thank u

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    1) You are assigning to a temporary object and not to the real object! You can't expect it to work when you are doing the actual assignment to a temporary object and returning it. The state of the class (or struct) itself doesn't change.
    2) Free members are different. Firstly they must be friend and secondly the left-hand argument (the first argument it takes) must be an instance of the class that it manipulates by reference.

    MyClass& operator = (MyClass& lhs, const T& rhs)

    Also note that the argument that operator = takes should be const, because it doesn't change it.
    Last edited by Elysia; 04-20-2009 at 10:03 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    71
    but i am also returning the temporary object...so shouldn't it go into num (in main) ???

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Yes, you are returning it but not using it. You would be using it if you assigned it to something else, a la:

    Code:
    Bignum a, b;
    a = b = "1234";
    Now a would be the temporary returned from b.

    Most often result of operator= is just ignored.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No.
    Operator = changes the state of the left-hand side, so in this case:
    num=s
    Num should be changed.
    Unfortunately it isn't. Instead the expression
    (num=s)
    returns a temporary object which is discarded (and it's also wrong).

    Also, operator = usually returns a reference itself:
    return *this;
    This allows stuff to be chained, such as:
    a = b = c = d;
    etc
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    37
    You can have the = operator do whatever you like. You can have it multiply instead of assign, or even launch a rocket, C++ will let you write whatever you want as the definition.

    If you want it to do an assignment, the reasonable expectation for its use, then you need to change the original value, not the temporary variable.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Fixes:
    str should be const char *
    return type should be bignum&
    remove the temp variable and all occurrences of temp.
    return *this
    You can also lose the .h

    Next step:
    Implement the default constructor and a constructor that also takes a const char * as an argument.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function not working
    By sloopy in forum C Programming
    Replies: 31
    Last Post: 11-12-2005, 08:08 PM
  2. Program Not working Right
    By raven420smoke in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2005, 03:21 AM
  3. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 07:47 PM
  4. x on upper right corner not working
    By caduardo21 in forum Windows Programming
    Replies: 1
    Last Post: 02-20-2005, 08:35 PM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 04:08 AM