Thread: namespace/class typedef problem

  1. #1
    Registered User Osaou's Avatar
    Join Date
    Nov 2004
    Location
    Stockholm, Sweden
    Posts
    69

    namespace/class typedef problem

    MS VC++ .NET complains about the following code:

    Code:
    namespace Osaou{
    	class Container{
    	private:
    
    		class Sub{
    		private:
    
    			typedef int *pInt;
    
    		protected:
    
    			void v_Test(void);
    		};
    	};
    }
    
    void Osaou::Container::Sub::v_Test(void){
    	Osaou::Container::Sub::pInt psdw_test = 7777;				// ERROR
    }
    Error message:
    error C2248: 'Osaou::Container::Sub::pInt : cannot access private typedef declared in class 'Osaou::Container::Sub'

    Please, help? =S
    Last edited by Osaou; 07-13-2006 at 10:12 PM.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    typedefs need to be public most of the time, because they need to be declared before you ever use them. You probably should declare the typedef outside the class, but still inside the namespace.

  3. #3
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Are you using VS 2003? When I try to compile your code I get the error:
    Code:
    error C2440: 'initializing' : cannot convert from 'int' to 'Osaou::Container::Sub::pInt'
    And no errors if I do:
    Code:
    namespace Osaou
    {
      class Container
      {
      private:
      
        class Sub
        {
          private:
            typedef int *pInt;
          protected:
            void v_Test(void);
        };
      
      };
    }
    
    void Osaou::Container::Sub::v_Test(void)
    {
      pInt psdw_test = 0;
    }
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    903
    I believe Osaou's way recquires the 'typename' keyword in front.

  5. #5
    Registered User Osaou's Avatar
    Join Date
    Nov 2004
    Location
    Stockholm, Sweden
    Posts
    69
    Well, actually, that's not the code I'm using... But the concept is the same... Here is the real code - albeit in a shorter version, stripped of everything that has no relevancy:

    In the header file (.h):

    Code:
    namespace Osaou{
    	typedef class Script{
    	public:
    
    		typedef class CppClass: public Osaou::Parser{
    		private:
    
    			typedef class Operator{
    			private:
    
    				unsigned int udw_return_flags;
    				unsigned int udw_arg_flags;
    				Osaou::Script::pOperator pO_op;
    
    			public:
    
    				Operator(void);
    			} *pOperator;
    
    		protected:
    
    			void v_AddOperator(char *psb_return, char *psb_op, char *psb_arg);
    		} *pCppClass;
    	} *pScript;
    }
    And here the actual script file (.cpp):

    Code:
    void Osaou::Script::CppClass::v_AddOperator(char *psb_return, char *psb_op, char *psb_arg){
    	// allocate memory for a new operator of this class
    	Osaou::Script::CppClass::pOperator pO_new = &LtO_operators.rt_PushBack();
    }
    The real error message is hence:
    error C2248: 'Osaou::Script::CppClass::pOperator' : cannot access private typedef declared in class 'Osaou::Script::CppClass'


    I might add that the Script class used to Not be in the Osaou namespace - instead named OsaouScript - and that then everything worked fine...
    By the way, I'm using VS .NET (2002)... Version 7.0.XX that is...

  6. #6
    Registered User Osaou's Avatar
    Join Date
    Nov 2004
    Location
    Stockholm, Sweden
    Posts
    69
    Fixed!

    JaWiB, I tried referring to pInt (really pOperator) without the namespace and class scope, and that worked fine...

    Is this "error" according to the C++ standard? I mean, should I not be able to refer to a class within a namespace with the scope operator when within that very namespace? I.E:

    Code:
    namespace NS{
    	class C{
    		void Test(){
    			NS::C *ptr;
    		}
    	};
    }
    Should this not work because we're already in the NS namespace?

  7. #7
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    I'm still not seeing where your error came from. That last code snippet you posted works fine for me, and the one before that would work if you defined Osaou::Parser, put a typedef of pOperator before the Operator class, and defined LtO_operators.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  8. #8
    Registered User Osaou's Avatar
    Join Date
    Nov 2004
    Location
    Stockholm, Sweden
    Posts
    69
    And yet it didn't... Well weird indeed.

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, 08:18 PM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. typedef problem
    By dr$brown in forum C Programming
    Replies: 6
    Last Post: 12-25-2004, 12:27 PM
  4. build errors migrated from dx9b to dx9c sdk
    By reanimated in forum Game Programming
    Replies: 4
    Last Post: 12-17-2004, 07:35 AM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM