Namespace within a class

This is a discussion on Namespace within a class within the C++ Programming forums, part of the General Programming Boards category; Hi guys, I'm having trouble getting a namespace to work from within a class definition. I'm not entirely sure this ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    29

    Namespace within a class

    Hi guys,

    I'm having trouble getting a namespace to work from within a class definition. I'm not entirely sure this is possible, but the ultimate goal is to create an object and pass it one of the enumerated values in its constructor. I've tried doing an it as below, but the compiler does not like that route.

    If I take out the namespace statement, then, in the main driver, I can create an object by calling pqueue test(pqueue::FIFO) (or any of the other three values)... Is there a way to do this with namespaces, so I can simply call pqueue test(FIFO)?

    Thanks!!

    -Max

    class definintion - printQueue.h
    Code:
    #include <iostream>
    
    using namespace std;
    
    class pqueue
    {
    public:
    	namespace p
    	{
    	enum pTypes {FIFO, JOBSIZE, PRIORITY}; //used for naming which type of printer
            }
    	
    	pqueue(pTypes type);
    	
    private:
    	pTypes printer_type;				//which type of printer this is
    	job *front;							//front of the queue
    	job *back;							//end of the queue
    
    };
    Class implementation - printQueue.cpp
    Code:
    #include <iostream>
    #include "printQueue.h"
    
    using namespace std;
    
    pqueue::pqueue(pTypes type)
    {
    	printer_type = type;
    	front = NULL;
    	back = NULL;
    }
    Program driver -- test.cpp
    Code:
    #include <iostream>
    #include "printQueue.h"
    
    using namespace std;
    using namespace p;
    
    int main()
    {
    	pqueue test(FIFO);
    
    }

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,252
    Quote Originally Posted by maxsthekat View Post
    If I take out the namespace statement, then, in the main driver, I can create an object by calling pqueue test(pqueue::FIFO) (or any of the other three values)... Is there a way to do this with namespaces, so I can simply call pqueue test(FIFO)?
    Yes. Put the enum outside the class completely.

    The syntax you want, where you can only say "FIFO" during a constructor call but not anywhere else, is not possible. It's all or nothing.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,993
    If you do not want the enum to be in the namespace of the class, you could define it in a separate namespace and thus use a using directive to get your desired effect (or just dump it in the global namespace to begin with, but that may not be advisable).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    29
    Works great. Thanks guys

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You may also review your policy of including <iostream> in every file, followed by using namespace std;
    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).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. LDAP Query
    By Travoiz in forum C++ Programming
    Replies: 0
    Last Post: 08-13-2009, 02:58 PM
  2. Replies: 8
    Last Post: 10-02-2005, 12:27 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM

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