Jeff Cotton

Members
  • Content count

    1
  • Joined

  • Last visited

Community Reputation

109 Neutral

About Jeff Cotton

  • Rank
    Newbie
  1. The problem with your new code is that you have memory leaks. You aren't de-allocating the memory you allocated with the new * calls. i.e. whoKnows *twelve = new whoKnows; twelve->setHidden(12); whoKnows *thirty = new whoKnows; thirty->setHidden(30); whoKnows *six = new whoKnows; six->setHidden(6);  You have several options to fix the problem the best is probably to use smart pointers in your container. However, if you go back to your original code you can use the iterators directly and everything should work fine. i.e. #include <iostream> #include <forward_list> using namespace std; class whoKnows { private: int hidden; public: int getHidden(); void setHidden(int newint); }; int whoKnows::getHidden() { return hidden; } void whoKnows::setHidden(int newint) { hidden = newint; } int main() { forward_list<whoKnows> test; whoKnows twelve; twelve.setHidden(12); whoKnows thirty; thirty.setHidden(30); whoKnows six; six.setHidden(6); test.push_front(twelve); test.push_front(thirty); test.push_front(six); cerr<<"Showing all values.\n"; for(auto it = test.begin(); it != test.end(); it++) { cerr<<"Class found, value: "<<(*it).getHidden()<<"\n"; } cerr<<"Increasing all values.\n"; for(auto it = test.begin(); it != test.end(); it++) { (*it).setHidden((*it).getHidden()+1); } cerr<<"Reshowing all values.\n"; for(auto it = test.begin(); it != test.end(); it++) { cerr<<"Class found, value: "<<(*it).getHidden()<<"\n"; } cout<<"Done\n"; return 0; }