-1

I have this as a member of the class TASK.

list<Stimulation*> listOfStimulations;

And the rest of my class definition;

class Task
{
public:
    Task();
    Task(const Task& obj);
    Task(const Task&& obj);
    Task(string, list<Stimulation*>);
    Task& operator+=( Stimulation* obj);
    Stimulation* Task::operator[](int i) const;
};
#endif

And I want to be ale to access each Stimulation in the list.

I know I should be using vector, but using list is a requirement.

How would I overload []?

I've tried

    Stimulation* Task::operator[](int i) const
{
    list<Stimulation*>::const_iterator iter;
    iter = listOfStimulations.begin();
    advance(iter, i);
    return *iter;
}

My main issue is that this code isn't letting me access the elements of my list.

For example, listOfStimulations[i] does not work, and won't let me access the function display within the Stimulations class.

user3701231
  • 45
  • 1
  • 8
  • just use `begin()` and `end()`? – Hatted Rooster Nov 25 '16 at 16:59
  • You could traversing the list with iterator until reached the nth node. – felix Nov 25 '16 at 17:00
  • If it's the standard `list`, you can't; `operator[]` must be a non-static member function. The requirement of `std::list` suggests that this exercise wants to teach you how to program without relying on indexing. – molbdnilo Nov 25 '16 at 17:02
  • @molbdnilo Sorry the exercise does specify to overload the [] operator. – user3701231 Nov 25 '16 at 17:04
  • @felix I've tried https://paste.ee/p/j64wi I'm not entirely sure if it does what I want. I want to be able to do listofStimulation[i]->display() to call the display function of the object. – user3701231 Nov 25 '16 at 17:06
  • @GillBates Excuse me if I'm wrong but doesn't that only use the first and last element? – user3701231 Nov 25 '16 at 17:08
  • You have to use them in an iterator check this http://stackoverflow.com/questions/5470331/traverse-a-list-using-an-iterator – Ol1v3r Nov 25 '16 at 17:12
  • @user3701231 You're not supposed to overload the operator for `std::list`, but for your own `Task`. Which it appears you have already done, so it's not clear what the question is.. – molbdnilo Nov 25 '16 at 17:20
  • Okay the [] operator is to access Stimulation objects. Stimulation objects within the list. I am unable to do that. – user3701231 Nov 25 '16 at 17:29
  • When you update a post like now with the advance leave the original code there. write update and post it under it. so people can follow the progression of the thread. – Ol1v3r Nov 25 '16 at 17:45
  • Okay, very sorry. New to Stack Overflow. Thanks for your help thus far. – user3701231 Nov 25 '16 at 17:47
  • @user3701231 I suspect there's a misunderstanding somewhere. You're supposed to use it like this: `Task t; t += some_stimulation; t[0]->display();`. You're not supposed to index the list inside `Task`. – molbdnilo Nov 25 '16 at 17:51
  • @molbdnilo The thing is I'm supposed to access Stimulation objects using [] not Task. The list stores Stimulation objects and is a member of the Task class. Sorry for being confusing – user3701231 Nov 25 '16 at 18:04
  • @molbdnilo Okay, I actually decided to fully grasp what you guys were saying. I completely understand now, thank you. – user3701231 Nov 25 '16 at 18:24
  • Off topic: It sounds like you are bound by assignment requirements but in the real world if you do a lot of iterating (as suggested by the `operator[]` requirement) as opposed to a lot of insert and remove, `std::list` may not be the tool you want to use. A more complete discussion: http://www.stroustrup.com/bs_faq.html#list – user4581301 Nov 25 '16 at 19:00
  • @user4581301 Yeah I guess it was more on the terms of learning about iterators. – user3701231 Nov 25 '16 at 19:04

2 Answers2

1

The best way to sequentially access a list is to use its Iterator

I suggest using the data structure that fits the requirements, and not trying to use a list just because.

This is how an Iterator works;

for (std::list<T>::iterator it = mylist.begin(); it != mylist.end(); ++it)
{
   if (...)
   {
      return *it;
   }
}

Now you can use advance to get "random" access;

std::list<T>::iterator it = mylist.begin();
std::advance(it,5);
return *it
Community
  • 1
  • 1
Ol1v3r
  • 758
  • 1
  • 10
  • 23
  • Indeed, please see https://paste.ee/p/j64wi the question is would this overload the [] for my list? or just my actual object. – user3701231 Nov 25 '16 at 17:18
  • Okay I've updated my code (see main post). I'm still having trouble access the display function for each stimulation. – user3701231 Nov 25 '16 at 17:37
  • try using the std::advance(it,5); the result of the std::advance(it,5); is the same as the result for mylist[5] – Ol1v3r Nov 25 '16 at 17:41
  • You're right it is, but it still doesn't let me access the members of the object inside the list. (Example, each object inside the list has the function display). I need to call that – user3701231 Nov 25 '16 at 17:45
  • what is the error you need to provide more information not just Not working... what is Display doing... how are you calling it.. and so on – Ol1v3r Nov 25 '16 at 17:46
  • It says no members available. display just outputs the private members inside the object. – user3701231 Nov 25 '16 at 17:48
  • stating the obvious but Is Display Public ? I suggest you closing this thread as it is no longer relevant to the first question asked and create another thread with the new question. That is why you are unable to access the members when using the above code. – Ol1v3r Nov 25 '16 at 17:51
  • yep indeed it is. – user3701231 Nov 25 '16 at 17:59
  • well I've actually been using an iterator before this thread, the reason why I posted was because it wasn't working – user3701231 Nov 25 '16 at 18:00
  • 1
    unconditional `return` in a `for` loop? – Hatted Rooster Nov 25 '16 at 19:40
0

You have not specified if you need a random access to each element in the list, given its index/position.

Anyway, as commented by molbdnilo, std::list has no operator[], for description of the reasons, please read this other question on StackOverflow.

The best you can do to access items in the list is using iterators, starting from std::list::begin().

edited
Ok, based on your own comment, you need random access at each index-th item. The code you linked in that comment looks like a good way to go.

Community
  • 1
  • 1
roalz
  • 2,699
  • 3
  • 25
  • 42
  • Hi, essentially I wish to take my list and access each element within the square brackets. I know the reasons as to why, and I should be using a vector. It is a requirement unfortunately. I've tried this, https://paste.ee/p/j64wi and it doesn't do what I want it to do. listofStimulations[i]->display() – user3701231 Nov 25 '16 at 17:12
  • The only problem is I can't seem to use the list to access each pointer of object within, which leads me to believe I've done somethign wrong with my declaration. – user3701231 Nov 25 '16 at 17:17
  • Without a complete yet basic source code, it's hard to try to understand what you are really doing and what is needed to help you. I suggest you to edit your question and add minimal source code to it (inline, not a linked page outside StackOverflow). In the code you linked you are overloading operator[] of a class called Task (not , which is not defined anywhere in your code fragment. – roalz Nov 25 '16 at 17:19