Thread: Namespace within a class

  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
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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
    28,413
    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).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 07:18 AM