Using DEQUE within Class Definition

This is a discussion on Using DEQUE within Class Definition within the C++ Programming forums, part of the General Programming Boards category; I have an array of stores with type class " manager " partially defined below: Code: private: char* LastName; char* ...

  1. #1
    Polar Fuzz
    Join Date
    Oct 2003
    Posts
    36

    Thumbs up Using DEQUE within Class Definition

    I have an array of stores with type class "manager" partially defined below:

    Code:
    private:
    	char* LastName;
    	char* Store;
    	char* Specialty;

    Driver program creates:

    Code:
    manager store [5];
    So, this is a storage array of 5 managers!

    I have another class (partially defined below) for customers:

    Code:
    private:
    	char* LastName;
    	char* Age;
    	char* Product;
    However, each manager will service each customer one at a time in a queue created for each manager, and each manager is part of a fixed array.

    I am having trouble creating and accessing a DEQUE created for the managers and accessible by the driver:

    Code:
    #include "Manager.h"
    #include "Customer.h"
    #include <deque>
    :
    :
    Manager::Manager()	{
    	LastName    = NULL;
    	Store	       = NULL;
    	Specialty	= NULL;
    	deque<Customer> CustomerQue;
    }
    
    Manager::Manager(char *last, char *store, char *specialty)	{
                :
                :
    	deque<Customer> CustomerQue;
    }
    
    void Manager::SetName (char* last)	{
    	
    	LastName = _strdup(last);
    }
    
    void Manager::SetCustomerQue (void)	{
    	deque<Customer> CustomerQue;
    }
    From the Driver, I want to be able to push a customer to the back or front of the manager queue using something similar to this:

    Code:
    store[0].CustomerQue.push_front("Williams", "20", "Bicycle");
    This obviously does not work and I am thinking it has to be simple. So, how build a deque within a class, accessible from the driver?

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Just fyi, I don't really know the C++ library at all, but can't you just make the deque object a pointer and assign it dynamically? It looks like you're doing something really stupid and allocating an object with the same type and name (but different scope) in the constructor and in your SetCustomerQue() function. If you want to be able to change deque objects on the fly, you should really be using a pointer, not an actual regular class-wide variable.... if that makes sense.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Code:
    store[0].CustomerQue.push_front(Customer("Williams", "20", "Bicycle"));
    Does your class have a proper copy constructor and copy assignment operator? If you have a destructor to free memory from those char*'s, you need copy functions. A better solution would be to use C++ strings.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    Manager::Manager(char *last, char *store, char *specialty)	
    // <- CustomerQue is default constructed at this point
    {
    	deque<Customer> CustomerQue;  //local object
    }
    
    void Manager::SetCustomerQue (void)	{
    	deque<Customer> CustomerQue;  //local object
    }
    Neither of these functions do anything other than declaring a local object whose name shadows the name of a data member. These go out of scope at the end of the method.

    If you want to clear a deque, it should have a clear method, and if you want to change the contents it currently holds, it should have the assignment operator (may also use swap).

    Code:
    store[0].CustomerQue.push_front(Customer("Williams", "20", "Bicycle"));
    The manager class might have a addCustomer method. This would be better because now the user of the class does not know the particular type of container Manager is storing Customers in, in case you want to change that later:
    Code:
    void Manager::addCustomer(const Customer& c)
    {
        CustomerQue.push_front(c);
    }
    
    ...
    store[0].addCustomer( Customer("Williams", "20", "Bicycle"));
    Of course, if you stick with char*, you'll need to write copy constructors, destructor and assignment operator.
    Last edited by anon; 10-01-2007 at 01:56 AM.
    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).

  5. #5
    Polar Fuzz
    Join Date
    Oct 2003
    Posts
    36

    Deque with Class definition

    Actually, I wanted to have the deque definition within a simple class definition (TestClass.h) but the line of code does not seem to work here:

    Code:
    #include <deque>
    
    class TestClass  {
    
    private:
       int var1;
       char  var2;
       double val3;
       deque<int> TestQue;  // This does not seem to work
    
    public:
              ..................};
    I get an error message, something about a semicolon that is expected before the "<". I am hoping to define the deque within the class definition (above) and I have tried public, private, above the private and nothing seems to work.

    Do you know how to define a deque within the class header (*.h)?

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Maybe std::deque<int>?

  7. #7
    Polar Fuzz
    Join Date
    Oct 2003
    Posts
    36
    Yep, that was it. I forgot to include "using namespace std;" !!!

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    You shouldn't use using-directives or using-declarations in header files, since they spill over into any file that includes them. Users of header files shouldn't need to be concerned about this.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I guess, if you are using something very often, you could use "typedef std::string mystring", and use mystring instead of string (where my in mystring is something relevant to the project, of course).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. help with class varialbe definition..
    By smartalco in forum C++ Programming
    Replies: 2
    Last Post: 04-27-2006, 04:37 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  4. Class Definition?
    By Jamina in forum C++ Programming
    Replies: 4
    Last Post: 08-08-2003, 12:12 AM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 02:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21