Thread: need help with overload operator[]

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    30

    Unhappy need help with overload operator[]

    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.but im not sure how?



    Code:
    //initial test code
    //replacing item at 0th to be 1
    
    v[0]=1;
    
    //so is it right if i'm assuming = as overload operator?
    //wouldn't it makes the test case to be
    
    v.operator[](index).operator=(value);
    but is that right?and how am i suppose to write the signature?

    thanks in advance.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You need to provide more context, e.g., what is v?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    30
    v is a vector<int> v.
    im doing it in class.
    i've done most of the part for the function
    Code:
    Vector &Vector ::operator [](const unsigned int index);
    but my prob is how can i get the value where the position is assigned to.

    like i said before the test case will be v[o]=1 and v is a vector.
    Last edited by effa; 10-09-2009 at 04:23 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by effa
    v is a vector<int> v.
    Wait, are you talking about std::vector, or your own vector class template? If it is your own vector class template, then what has that got to do with your Vector class?

    Quote Originally Posted by effa
    i've done most of the part for the function
    You should be aware that that means that given a non-const Vector object vec, vec[0] returns a reference to a Vector...

    Quote Originally Posted by effa
    but my prob is how can i get the value where the position is assigned to.
    ... if so, then you presumably would have operator= overloaded for your Vector class such that it can take an int argument, or you have a constructor that can convert an int to a Vector implicitly.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I'm quite sure you need to return a reference to that item in the Vector, not the Vector itself.
    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).

  6. #6
    Registered User
    Join Date
    Aug 2009
    Posts
    30
    Code:
    Vector &Vector ::operator =(const Vector &num);
    doesnt the argument for the operator = have to be a Vector& not an int.
    i tried to use pointer but it didnt work.

    i also even tried to create a new function for v[0]=1 and make *edit as a private member function so that the function for operator[] will take the value that it is assign for.


    Code:
    Vector &Vector ::operator =(const int &num){
    
    	*edit= num;
    
    	return *edit;
    }
    could you please explain where did i do wrong here?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by effa
    doesnt the argument for the operator = have to be a Vector& not an int.
    If the parameter for operator= is a (const) reference to a Vector, then that operator= is a copy assignment operator. However, that does not restrict you from overloading operator= to take arguments of other types, such as int.

    That said, anon stated what I was trying to hint at: that you might want to check if the return type of your operator[] is correct.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Aug 2009
    Posts
    30
    urgh dont quite understand what anon trying ti say there

    return a reference to that item in the Vector, not the Vector itself
    mind to explain to me how this works? really confuse now. =_=

    and what do you mean with the return type must be correct.what i did for the function operator[ ] is returning a list of int that is being modified after the v[0]=1.

  9. #9
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    Suppose your vector contains values of type T, then the [] operator must return a reference to T and not the Vector class itself.

    what do you get when you use the [] operator with the stl vector ?

    eg:
    Code:
    vector<T> vec(100);
    T val = vec[0];
    so, your's should act the same way too.
    Spidey out!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. overload *
    By shuo in forum C++ Programming
    Replies: 5
    Last Post: 06-10-2007, 04:44 AM
  2. Having trouble with operator*=()
    By Lurker in forum C++ Programming
    Replies: 10
    Last Post: 10-26-2003, 03:03 PM
  3. Buffer Overload
    By xddxogm3 in forum C++ Programming
    Replies: 1
    Last Post: 09-25-2003, 03:21 PM
  4. overload new and delete
    By Roaring_Tiger in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2003, 07:48 PM
  5. C++ Operator Overload Question
    By cworld in forum C++ Programming
    Replies: 0
    Last Post: 03-25-2002, 07:17 PM