combining two overloads operators

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hanna88
    New Member
    • Oct 2009
    • 33

    combining two overloads operators

    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

    Code:
    v.operator[](index).operator=(value);
    but is that right?and how am i suppose to write the signature?

    thanks in advance.
  • hanna88
    New Member
    • Oct 2009
    • 33

    #2
    overload operators problem

    here's the error i have in the test code:
    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?

    Comment

    • newb16
      Contributor
      • Jul 2008
      • 687

      #3
      you have something like this in your code:

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

      and then compiler wants to assign int to myvalue
      - you need to have
      myvalue& myvalue::operat or=(int x)

      Comment

      • JonathanS
        New Member
        • Jan 2008
        • 37

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

        Comment

        • hanna88
          New Member
          • Oct 2009
          • 33

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

          Comment

          • hanna88
            New Member
            • Oct 2009
            • 33

            #6
            myvalue& myvalue::operat or=(int x)
            i got an error of

            error C2440: 'return' : cannot convert from 'int' to 'Vector &'
            where did i do wrong?

            Comment

            • newb16
              Contributor
              • Jul 2008
              • 687

              #7
              It depends on what _exactly_ code you tried to compile.

              Comment

              Working...