Connecting Tech Pros Worldwide Forums | Help | Site Map

combining two overloads operators

Member
 
Join Date: Oct 2009
Posts: 33
#1: Oct 9 '09
here's the problem.
i was given a test case v[0]=1.
in order to implement operator[],i need to get the access the value it is refering to which is 1.
i tried to write some code ignoring the = by assuming the compiler knows that = means assigning 1 to that position 0.and it turn out to be wrong.

so is it right if i'm assuming = as overload operator?
wouldn't it makes the test case to be

Expand|Select|Wrap|Line Numbers
  1.  
  2. v.operator[](index).operator=(value);
  3.  
  4.  
but is that right?and how am i suppose to write the signature?

thanks in advance.

Member
 
Join Date: Oct 2009
Posts: 33
#2: Oct 9 '09

re: combining two overloads operators


here's the error i have in the test code:
Quote:
error C2679: binary '=' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion)
any hints to get the value that it is refering to?
Needs Regular Fix
 
Join Date: Jul 2008
Posts: 386
#3: Oct 9 '09

re: combining two overloads operators


you have something like this in your code:

myvalue& myvector::operator[] (const int index)...

and then compiler wants to assign int to myvalue
- you need to have
myvalue& myvalue::operator=(int x)
Member
 
Join Date: Jan 2008
Posts: 38
#4: Oct 9 '09

re: combining two overloads operators


Check this out for the [ ] overloading http://www.learncpp.com/cpp-tutorial...ript-operator/

I don't think you need to additionally overload the assignment operator for this particular case. Once you are accessing any particular element (by using the [ ] with the index) you can then assign to it with = as normal.
Member
 
Join Date: Oct 2009
Posts: 33
#5: Oct 9 '09

re: combining two overloads operators


thanks for your reply.

im doing this using class.so should i make int x as a private member in order the function for operator[] to get the value x.
Member
 
Join Date: Oct 2009
Posts: 33
#6: Oct 9 '09

re: combining two overloads operators


Quote:
myvalue& myvalue::operator=(int x)
i got an error of

Quote:
error C2440: 'return' : cannot convert from 'int' to 'Vector &'
where did i do wrong?
Needs Regular Fix
 
Join Date: Jul 2008
Posts: 386
#7: Oct 9 '09

re: combining two overloads operators


It depends on what _exactly_ code you tried to compile.
Reply