queue

This is a discussion on queue within the C++ Programming forums, part of the General Programming Boards category; If I do this : Code: queue<Ccar *>q; cout << q.empty(); my class works ok If I do Code: static ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    291

    queue

    If I do this :
    Code:
    queue<Ccar *>q; 
    cout << q.empty();
    my class works ok

    If I do
    Code:
    static queue<Ccar *>q; 
    cout << q.empty();
    I get undefined reference to Ccar::q - what am I doing wrong ?

  2. #2
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    Why do you want to make your queue static in the first place?
    Mr. C: Author and Instructor

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    If I use a queue<Ccar *>q; then each object of the Ccar class will have it's own queue. I'd like to have a shared queue that all object of Ccar can access.

  4. #4
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    Unhappy

    You can make data members and member functions static- but I have not seen making classes static. (Can' t do this is in Java either.)
    Mr. C: Author and Instructor

  5. #5
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Originally posted by Mister C
    You can make data members and member functions static- but I have not seen making classes static. (Can' t do this is in Java either.)
    I don't think that's what he meant.

    This works:

    Code:
    class Ccar
    {
        public:
        //shared queue
        static std::queue<Ccar*> q;
    };
    
    //place this definition in a .cpp file, not a header
    std::queue<Ccar*> Ccar::q;
    
    main()
    {
        //local static variable
        static std::queue<Ccar*> p;
        cout << Ccar::q.empty() << p.empty();
    }
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  6. #6
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    Wink

    I got it and understand- I hope laasunde does! Sometimes knowing what a person wants done helps. I would do it the way you have done.
    Mr. C: Author and Instructor

  7. #7
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    Thanks Sang-drax

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with FIFO QUEUE
    By jackfraust in forum C++ Programming
    Replies: 23
    Last Post: 04-03-2009, 09:17 AM
  2. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 03:55 AM
  3. help with queues
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 05-21-2002, 10:09 PM
  4. help with queues
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 05-21-2002, 12:39 PM
  5. queue help
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-29-2001, 09:38 AM

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