C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Typedef

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Pramod
Guest





PostPosted: Thu Feb 16, 2006 3:06 am    Post subject: Typedef Reply with quote



Hi,

I am new to C++ . I was trying to compile following Class which just
has a map from integer error code to error description. I am not sure
of the complier error.

Why compiler flags error for typedef present in *private* area of the
class. If I move the typedef to *public* are it complies fine.

RegExp.h
--------------
class RegError {

public:
RegError( int ErrorCode ) ;
const std::string what ( ) ;

private:
int regerror;
typedef std::map< int , std::string > ErrorMap;
static void InitializeErrorMap ( );
static ErrorMap RegExpError;
};


RegExp.C
-----------------
static RegError::ErrorMap RegExpError;


=================================================================

Compiler g++
------------------
RegExp.h:16: error: 'typedef class std::map<int, std::string,
std::less<int>, std::allocator<std::pair<const int, std::string> > >
RegError::ErrorMap' is private
RegExp.C:4: error: within this context


Compiler aCC
---------------
Error 182: "RegExp.C", line 4 #
"std::map<int,std::basic_string<char,std::char_traits<char>,std::allocator<char>
Quote:
,std::less<int>,std::allocator<std::pair<const
int,std::basic_string<char,std::char_traits<char>,std::allocator<char

RegExpError" cannot access private member "typedef

std::map<int,std::basic_string<char,std::char_traits<char>,std::allocator<char>
Quote:
,std::less<int>,std::allocator<std::pair<const
int,std::basic_string<char,std::char_traits<char>,std::allocator<char

RegError::ErrorMap".

static RegError::ErrorMap RegExpError;
^^^^^^^^^^^^^^^^^^

Thanks in advance.
Pramod


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Guest






PostPosted: Sat Feb 18, 2006 1:06 am    Post subject: Re: Typedef Reply with quote



Pramod wrote:
Quote:
RegExp.C
-----------------
static RegError::ErrorMap RegExpError;

remove "static":
RegError::ErrorMap RegExpError;


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
werasm
Guest





PostPosted: Sat Feb 18, 2006 1:06 am    Post subject: Re: Typedef Reply with quote



Pramod wrote:
Quote:
Hi,

Why compiler flags error for typedef present in *private* area of the
class. If I move the typedef to *public* are it complies fine.

IMHO this should not make a difference as you are only providing a
definition for the static member. Therefore you must be able to qualify
the type under scrutiny. I did not notice

....
#include <map>
#include <string>
....

in your code.

Quote:

RegExp.h
--------------
class RegError {

public:
RegError( int ErrorCode ) ;
const std::string what ( ) ;

private:
int regerror;
typedef std::map< int , std::string > ErrorMap;
static void InitializeErrorMap ( );
static ErrorMap RegExpError;
};



RegExp.C
-----------------
static RegError::ErrorMap RegExpError;

<static> should not be used here...

RegError::ErrorMap RegError::RegExpError; //should do!

You forgot to qualify the class member RegExpError, therefore you did
not provide a definition for the static member but actually
declared/defined a global member called RegExpError. Good thing you
made the typedef private, else the code would have done something other
than you've expected Smile.

Werner


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Audison.Athena
Guest





PostPosted: Sat Feb 18, 2006 1:06 am    Post subject: Re: Typedef Reply with quote

because access control applies to name, if access control is applied to
a typedef name, only the accessibility of the typedef name itself is
considered. The accessibiliy of the entity
of the entity referred to by the typedef is not considered.
so the access control is only apply to the typedef name "ErrorMap"
itself, but not apply to the entity "std::map< int , std::string >"
referred to by "ErrorMap". so you aught to put the typedef declaration
to public field.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Guest






PostPosted: Sat Feb 18, 2006 2:06 am    Post subject: Re: Typedef Reply with quote

Pramod wrote:
Quote:
Hi,

I am new to C++ . I was trying to compile following Class which just
has a map from integer error code to error description. I am not sure
of the complier error.

Why compiler flags error for typedef present in *private* area of the
class. If I move the typedef to *public* are it complies fine.

RegExp.h
--------------
class RegError {

public:
RegError( int ErrorCode ) ;
const std::string what ( ) ;

private:
int regerror;
typedef std::map< int , std::string > ErrorMap;
static void InitializeErrorMap ( );
static ErrorMap RegExpError;
};


RegExp.C
-----------------
static RegError::ErrorMap RegExpError;



Similarly,

static RegError::ErrorMap someOtherErrorMap;

wouldn't compile because RegError::ErrorMap is a private type.
try, instead:

static RegError::ErrorMap RegError::RegExpError;

see the missing RegError::?


Tony


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Heinz Ozwirk
Guest





PostPosted: Sat Feb 18, 2006 5:06 am    Post subject: Re: Typedef Reply with quote

"Pramod" <pramod.shimoga (AT) gmail (DOT) com> schrieb im Newsbeitrag
news:1140025917.257521.116880 (AT) g14g2000cwa (DOT) googlegroups.com...
Quote:
Hi,

I am new to C++ . I was trying to compile following Class which just
has a map from integer error code to error description. I am not sure
of the complier error.

Why compiler flags error for typedef present in *private* area of the
class. If I move the typedef to *public* are it complies fine.

If something is defined in the private section of a class, it is private and
must not be used outside its class. If it is defined in the public section,
it is public and may be used everywhere. That's what public and private have
been invented for. So, of cause you get an error if you try to use something
private outside its class, and of cause this error goes away when you make
it public.

Quote:

RegExp.h
--------------
class RegError {

public:
RegError( int ErrorCode ) ;
const std::string what ( ) ;

private:
int regerror;
typedef std::map< int , std::string > ErrorMap;
static void InitializeErrorMap ( );
static ErrorMap RegExpError;
};


RegExp.C
-----------------
static RegError::ErrorMap RegExpError;

This defines a global variable of type RegError::ErrorMap, and since
ErrorMap has been declared private in RegError you cannot use that type
outside the class. Probably that should have been a definition for
RegError::RegExpError, but such a definition should be written as

RegError::ErrorMap RegError::RegExpError;

HTH
Heinz



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Marco Wahl
Guest





PostPosted: Sat Feb 18, 2006 5:06 am    Post subject: Re: Typedef Reply with quote

"Pramod" <pramod.shimoga (AT) gmail (DOT) com> writes:
Quote:
Why compiler flags error for typedef present in *private* area of the
class. If I move the typedef to *public* are it complies fine.
^^^^^^^^

Hehe... 'Comply' is a fresh alternative to 'compile'.

Typedefs in the *private* area are not accessable from the outside (if
you don't use 'friend'). That's simply a part of the definition of
the C++-language. It's the same with member-variables and
member-function.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
viscroad
Guest





PostPosted: Sat Feb 18, 2006 5:06 am    Post subject: Re: Typedef Reply with quote

private class member can't invoke out the class scope.
"typedef std::map< int , std::string > ErrorMap;" is a private class
member, so you can't use it out of the class scope.
only the class member and friend member can use the *private* member.

Quote:
Why compiler flags error for typedef present in *private* area of the
class. If I move the typedef to *public* are it complies fine.
you put it into *public* area, so, we can use it.



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
jeroen
Guest





PostPosted: Sat Feb 18, 2006 5:06 am    Post subject: Re: Typedef Reply with quote

Don't have the time to really check this out, but to initialise a
static member of a class you'll have to name the class too, like this
(Note that RegError is mentioned twice, once for the type and once
again for the name of the member-var):

RegExp.C
--------
static RegError::ErrorMap RegError::RegExpError;

In your code you tried to create a new static variable of a private
type, which is not allowed.

Regards,
Jeroen.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Ulrich Eckhardt
Guest





PostPosted: Sat Feb 18, 2006 5:06 am    Post subject: Re: Typedef Reply with quote

Pramod wrote (in short):
Quote:
class RegError {
private:
typedef std::map< int , std::string > ErrorMap;
static ErrorMap RegExpError;
};

static RegError::ErrorMap RegExpError;

Here, you try to create an ErrorMap called RegExpError (i.e. in the global
namespace) and with static linkage. This has nothing to do with the equally
named object inside class RegError!

What you want is probably to define the latter, the syntax for that is
RegError::ErrorMap RegError::RegExpError;

cheers

Uli


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.