Thread: Cannot use any List operations(using Ubuntu)

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    5

    Cannot use any List operations(using Ubuntu)

    Hello everyone, I'm trying to construct a header class call Shopper.h, and inside the class I created a list and trying to manipulate using list operation, but every time I try to use any list operation, it doesn't comply. But when i used list operations on cpp file, it works.

    Code:
    #include <string>
    #include <iostream>
    #include <list>
    using namespace std;
    
    class Shopper{
    
    public:
      Shopper(string &n,string &p,list<string> L)
        {
          name = n;
          pro = p;
          shopList = L;
        {
        
        
    
      void delShop()
      {
        list<Shopper>::iterator i;
        i = shopList.begin();
        i =  shopList.erase(i);
      }
    
    //Return the first shop on the list
    string firstShop()
    {
      return shopList.front();
    }
    
    string getPro()
    {
      return pro;
    }
    
    string getName()
    {
      return name;
    }
    
    private:
     string name;
      string pro;
      list<string> shopList;
    };
    Last edited by samsun387; 06-03-2010 at 10:47 PM.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    If you set that iterator in the constructor, then add things to the list and try to use the iterator, it probably won't work (esp if you set it before there was anything in the list). Iterators are intended to be set and used right away -- you can't save them, work with the data structure for a while, and then expect them to serve much of a purpose unless you reset them.
    Last edited by MK27; 06-03-2010 at 04:35 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> every time I try to use any list operation, it doesn't comply
    What does that mean? Do you get a compiler error? Does the program fail? Does it crash?

  4. #4
    Registered User
    Join Date
    Jun 2010
    Posts
    5
    Quote Originally Posted by Daved View Post
    >> every time I try to use any list operation, it doesn't comply
    What does that mean? Do you get a compiler error? Does the program fail? Does it crash?
    Sorry, I meant complier error.

    Code:
     error: no match for ‘operator=’ in ‘((Shopper*)this)->Shopper::it1 = ((Shopper*)this)->Shopper::shopList.std::list<_Tp, _Alloc>::begin [with _Tp = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Alloc = std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >]()’
    /usr/include/c++/4.4/bits/stl_list.h:114: note: candidates are: std::_List_iterator<int>& std::_List_iterator<int>::operator=(const std::_List_iterator<int>&)
    Shopper.h: In function ‘void delShop(int)’:
    Shopper.h:24: error: ‘shopList’ was not declared in this scope.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    shopList is declared as a list of string, not a list of Shopper.
    Besides, all you need is:
    Code:
    void delShop()
    {
        shopList.pop_front();
    }
    Make sure you learn about const-correctness and constructor initialisation lists also.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Jun 2010
    Posts
    5
    Problem solved, Thank you so much
    Yes, i am trying to learn c++ and this is my first C++ program. Thank you for your help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c program that accepts and executes commands?
    By Cimposter in forum C Programming
    Replies: 3
    Last Post: 09-30-2009, 02:58 PM
  2. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 09:01 AM
  3. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  4. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 05:20 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM