Thread: [Dev-C++] Demystifying namespace
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Maya <Esc...@ne...> - 2003-12-18 03:41:49
|
I have a little bit of a problem with 'namespace'. Here is my problem:
namespace foo{
#define file __func__
#define line __LINE__
enum error_t {one, two, three};
}
namespace foo{
class loo{
public:
loo(){
std::string f = foo::file;
size_t l = foo::line;
std::cout << "file: " << f << " line: " << l << std::endl;
std::cout << foo::one << foo::two << foo::three << std::endl;
}
};
}
These std::cout's give me a parse error, what am I doing wrong?
--
FYI
http://www.astro.umd.edu/~marshall/abbrev.html
E-Mail Policy
http://www.vif.com/users/escalante/Email_Policy.html
* You cannot exercise your power to a point of humiliation.
- Jean Chretien
* The media's the most powerful entity on earth. They have the
power to make the innocent guilty and to make the guilty
innocent, and that's power.
- Malcom X
* "Innocent until proven guilty", no... not in Canada!!
|
|
From: P.D. v. Z. <pd...@xs...> - 2003-12-18 07:41:32
|
You shouldn't use #defines. #defines are cross-everything, they don't care
about scope, namespaces, functions, whatever. It's just a simple global
search&replace operation performed by the preprocessor. If you want your
namespace code to work, change it to something like:
namespace foo{
const std::string file = __func__;
const size_t line = __LINE__;
}
Note that I didn't test this though, but it should work (and didn't you mean
__FILE__ instead of __func__ there?)
Regards,
Paul
-----Original Message-----
From: dev...@li...
[mailto:dev...@li...] On Behalf Of Maya
Sent: Thursday, December 18, 2003 7:49 AM
To: C/C++ Programmers; Dev - C++
Subject: [Dev-C++] Demystifying namespace
I have a little bit of a problem with 'namespace'. Here is my problem:
namespace foo{
#define file __func__
#define line __LINE__
enum error_t {one, two, three};
}
namespace foo{
class loo{
public:
loo(){
std::string f = foo::file;
size_t l = foo::line;
std::cout << "file: " << f << " line: " << l << std::endl;
std::cout << foo::one << foo::two << foo::three << std::endl;
}
};
}
These std::cout's give me a parse error, what am I doing wrong?
--
FYI
http://www.astro.umd.edu/~marshall/abbrev.html
E-Mail Policy
http://www.vif.com/users/escalante/Email_Policy.html
* You cannot exercise your power to a point of humiliation.
- Jean Chretien
* The media's the most powerful entity on earth. They have the
power to make the innocent guilty and to make the guilty
innocent, and that's power.
- Malcom X
* "Innocent until proven guilty", no... not in Canada!!
-------------------------------------------------------
This SF.net email is sponsored by: IBM Linux Tutorials.
Become an expert in LINUX or just sharpen your skills. Sign up for IBM's
Free Linux Tutorials. Learn everything from the bash shell to sys admin.
Click now! http://ads.osdn.com/?ad_id=1278&alloc_id=3371&op=click
_______________________________________________
Dev-cpp-users mailing list
Dev...@li...
TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
|
|
From: Maya <Esc...@ne...> - 2003-12-18 17:26:30
|
I want to replace the word __func__ with the word file, and you are
telling me to assign the value of __func__ to the std::string variable?
Gotta check those books again, eh!
pd...@xs... wrote:
>You shouldn't use #defines. #defines are cross-everything, they don't care
>about scope, namespaces, functions, whatever. It's just a simple global
>search&replace operation performed by the preprocessor. If you want your
>namespace code to work, change it to something like:
>
>namespace foo{
> const std::string file = __func__;
> const size_t line = __LINE__;
>}
>
>Note that I didn't test this though, but it should work (and didn't you mean
>__FILE__ instead of __func__ there?)
>
>Regards,
>
>
>Paul
>
>-----Original Message-----
>From: dev...@li...
>[mailto:dev...@li...] On Behalf Of Maya
>Sent: Thursday, December 18, 2003 7:49 AM
>To: C/C++ Programmers; Dev - C++
>Subject: [Dev-C++] Demystifying namespace
>
>I have a little bit of a problem with 'namespace'. Here is my problem:
>namespace foo{
> #define file __func__
> #define line __LINE__
> enum error_t {one, two, three};
>}
>namespace foo{
> class loo{
> public:
> loo(){
> std::string f = foo::file;
> size_t l = foo::line;
> std::cout << "file: " << f << " line: " << l << std::endl;
> std::cout << foo::one << foo::two << foo::three << std::endl;
> }
> };
>}
>These std::cout's give me a parse error, what am I doing wrong?
>
>
--
FYI
http://www.astro.umd.edu/~marshall/abbrev.html
E-Mail Policy
http://www.vif.com/users/escalante/Email_Policy.html
* You cannot exercise your power to a point of humiliation.
- Jean Chretien
* The media's the most powerful entity on earth. They have the
power to make the innocent guilty and to make the guilty
innocent, and that's power.
- Malcom X
* "Innocent until proven guilty", no... not in Canada!!
|
|
From: <ce...@te...> - 2003-12-18 18:29:38
|
For what I see, your code is doing exactly the same, but without using
those #defines
Maya wrote:
> I want to replace the word __func__ with the word file, and you are
> telling me to assign the value of __func__ to the std::string variable?
>
> Gotta check those books again, eh!
>
> pd...@xs... wrote:
>
>> You shouldn't use #defines. #defines are cross-everything, they don't
>> care
>> about scope, namespaces, functions, whatever. It's just a simple global
>> search&replace operation performed by the preprocessor. If you want your
>> namespace code to work, change it to something like:
>>
>> namespace foo{
>> const std::string file = __func__;
>> const size_t line = __LINE__;
>> }
>> Note that I didn't test this though, but it should work (and didn't
>> you mean
>> __FILE__ instead of __func__ there?)
>>
>> Regards,
>>
>>
>> Paul
>>
>> -----Original Message-----
>> From: dev...@li...
>> [mailto:dev...@li...] On Behalf Of Maya
>> Sent: Thursday, December 18, 2003 7:49 AM
>> To: C/C++ Programmers; Dev - C++
>> Subject: [Dev-C++] Demystifying namespace
>>
>> I have a little bit of a problem with 'namespace'. Here is my problem:
>> namespace foo{
>> #define file __func__
>> #define line __LINE__
>> enum error_t {one, two, three};
>> }
>> namespace foo{
>> class loo{
>> public:
>> loo(){
>> std::string f = foo::file;
>> size_t l = foo::line;
>> std::cout << "file: " << f << " line: " << l << std::endl;
>> std::cout << foo::one << foo::two << foo::three <<
>> std::endl;
>> }
>> };
>> }
>> These std::cout's give me a parse error, what am I doing wrong?
>>
>>
>
|
|
From: <ce...@te...> - 2003-12-18 22:23:44
|
Yeah, I understand that perfectly.
I understood the last maya's question as "passing them (__func__ and
__line__) to a srting!!!??? What you say passes the value, I wanted to
pass the variable name*". The answer, is that her code, was intending to
do just that an std:string variable to which a __func__ or __liner__
were to be passed..
It was like her was thinking that when passing the #defined name for
them the item passed to her string variables where something like the
name of the "definition" variable, which has no sense. Hum, in that
moment I saw that sense of her question clearly, know, I wonder if I
really understood her question.
And yes, the errors should appear there, but I've noticed mingw has some
tendency to point the errors related to #defined thing in the next
lines; and thos related to missing ';' in headers to the begining of the
source file which includes the header.
Thanks.
P.S. - The high cross-posting comes from her message, I simply clicked
'reply all'
*which translated to code, say
__line__ = "this is the line blah" (sorry if this has no sense, but I'd
never used this);
She thinks,
if #define foo __line__
std::string yuu = foo; ----> yuu = "__line__" (which makes no sense)
instead of "this is the line blah".
Lars Hallberg wrote:
> Carlos García del Monte wrote:
>
>> For what I see, your code is doing exactly the same, but without
>> using those #defines
>
>
>
> The strange thing is that You get the error in the std::cout. You
> suposed tho get them erlier.
>
> #define declare preprosessor constant ('dumb' text string constants)
> that is global, they shuld not care about scope. They are replaced
> litary as text. Your code:
>
> std::string f = foo::file;
> size_t l = foo::line;
>
> Transforme to:
>
> std::string f = foo::__func__;
> size_t l = foo::__LINE__;
>
> or rather, the value of thes 'special' constants at the #define. The
> compile shuld likely find errors there :-/
>
> Maya:s code actuly declare thes constants as 'real' constants in the
> scoop foo, shuld make all the differens!
>
> PS Litle *too* much crosposting this, is it not? DS
>
> HTH /LaH
>
>>
>> Maya wrote:
>>
>>> I want to replace the word __func__ with the word file, and you are
>>> telling me to assign the value of __func__ to the std::string variable?
>>
>
|
|
From: Maya <Esc...@ne...> - 2003-12-19 01:21:09
|
Ok, see that there are a few misunderstandings here. I know that using
the preprocessor '#define' means kind of "replace", so in my statement
'#define line __LINE__' what I am saying is: the next time I use
'line' what I really mean is '__LINE__', but in my rush to finish the
program I started to treat 'line' as a variable, i.e. std::cout <<
jme::line << std::endl which of course is wrong, that is clear to me now.
I have handed in the program at school, and even though it works, I
still have a lot of questions about 'namespace'. I know what you are
thinking, ask the teachers! that's what my dad tells me too. Well I
have, but the teachers in my high school are too preoccupied with the
Christmas stuff to explain it in way that makes sense to ME and not only
to THEM. I mean. So, any help from you guys will be appreciated.
Please don't be rude if I ask questions that are very simple to you, and
above all, don't take me wrong if I say something that might sound
insulting; it is never my intention to be rude to anyone.
I share my email with my dad; he was really P.O. about some guy wring
obscenities to me. I mean, think about what would you feel if someone
starts using that kind of language with your kids, eh!
Hava a good Ho Ho Ho
This class is part of a program I wanted to hand in at school, but as we
all know
=====================
=== GCC ===
namespace jme {
#define method __PRETTY_FUNCTION__
#define file __FILE__
#define line __LINE__
#define function __func__
enum error_t{
//Generic error codes
msg, //Default message
warning, //Warning
fatal, //Display fatal and exit
.....
};
class Exception{
protected:
error_t error;
std::string File, Method;
size_t Line;
public:
//Setters
void setException( jme::error_t, e // Error code
const std::string&, fn // File name
const std::string&, m // Method
size_t l) // Line
{ error = e; File = fn; Method = m; Line = l; }
};// Exception
} // namespace
=======================================
--- Someprogram.H -----
namespace jme{
class SomepogramEx : virtual public jme::Exception{
public:
SomeprogramEx(error_t ec, const std::string& f,
const std::string& m, size_t l);
};//End of SomeprogramEx
class Someprogram{
std::string* str_example;
public:
Someprogram(){
try{str_example = new std::string;}
catch(std::bad_alloc& x){
// This works!!!!
throw jme::SomeprogramEx(msg, file,
method, line);
// This doesn't ?????
throw jme::SomeprogramEx(jme::msg,
jme::file, jme::method, jme::line);
}
}
}; //Someprogram
} // namespace
ce...@te... wrote:
> Yeah, I understand that perfectly.
> I understood the last maya's question as "passing them (__func__ and
> __line__) to a srting!!!??? What you say passes the value, I wanted to
> pass the variable name*". The answer, is that her code, was intending
> to do just that an std:string variable to which a __func__ or
> __liner__ were to be passed..
> It was like her was thinking that when passing the #defined name for
> them the item passed to her string variables where something like the
> name of the "definition" variable, which has no sense. Hum, in that
> moment I saw that sense of her question clearly, know, I wonder if I
> really understood her question.
>
> And yes, the errors should appear there, but I've noticed mingw has
> some tendency to point the errors related to #defined thing in the
> next lines; and thos related to missing ';' in headers to the begining
> of the source file which includes the header.
>
> Thanks.
>
> P.S. - The high cross-posting comes from her message, I simply clicked
> 'reply all'
>
> *which translated to code, say
> __line__ = "this is the line blah" (sorry if this has no sense, but
> I'd never used this);
> She thinks,
> if #define foo __line__
> std::string yuu = foo; ----> yuu = "__line__" (which makes no sense)
> instead of "this is the line blah".
>
> Lars Hallberg wrote:
>
>> Carlos García del Monte wrote:
>>
>>> For what I see, your code is doing exactly the same, but without
>>> using those #defines
>>
>>
>>
>>
>> The strange thing is that You get the error in the std::cout. You
>> suposed tho get them erlier.
>>
>> #define declare preprosessor constant ('dumb' text string constants)
>> that is global, they shuld not care about scope. They are replaced
>> litary as text. Your code:
>>
>> std::string f = foo::file;
>> size_t l = foo::line;
>>
>> Transforme to:
>>
>> std::string f = foo::__func__;
>> size_t l = foo::__LINE__;
>>
>> or rather, the value of thes 'special' constants at the #define. The
>> compile shuld likely find errors there :-/
>>
>> Maya:s code actuly declare thes constants as 'real' constants in the
>> scoop foo, shuld make all the differens!
>>
>> PS Litle *too* much crosposting this, is it not? DS
>>
>> HTH /LaH
>>
>>>
>>> Maya wrote:
>>>
>>>> I want to replace the word __func__ with the word file, and you are
>>>> telling me to assign the value of __func__ to the std::string
>>>> variable?
>>>
>>>
>>
>
>
>
> -------------------------------------------------------
> This SF.net email is sponsored by: IBM Linux Tutorials.
> Become an expert in LINUX or just sharpen your skills. Sign up for IBM's
> Free Linux Tutorials. Learn everything from the bash shell to sys admin.
> Click now! http://ads.osdn.com/?ad_id=1278&alloc_id=3371&op=click
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
--
FYI
http://www.astro.umd.edu/~marshall/abbrev.html
E-Mail Policy
http://www.vif.com/users/escalante/Email_Policy.html
* You cannot exercise your power to a point of humiliation.
- Jean Chretien
* The media's the most powerful entity on earth. They have the
power to make the innocent guilty and to make the guilty
innocent, and that's power.
- Malcom X
* "Innocent until proven guilty", no... not in Canada!!
|
|
From: Per W. <pw...@ia...> - 2003-12-19 01:41:59
|
1)
Please. Take as a custom to always use ALL CAPS when #define symbols. It
is practically impossible to read the code if you don't. Since a #defined
symbol might be just about any type of expansion it is really important
when reading the code.
2)
Since a #defined symbol is a preprocessor construct, they are never - I
repeat NEVER - inside any namespace. If the compiler have seen the
#define, and it hasn't been terminated by an #undefine, it will be used.
The compiler complains, since you try jme::file, jme::line, ... where file
and line are #defines.
When you write jme::file it represents jme::__FILE__ which is expanded to
jme::"filename" which isn't a valid construct.
/Per W
On Thu, 18 Dec 2003, Maya wrote:
> Ok, see that there are a few misunderstandings here. I know that using
> the preprocessor '#define' means kind of "replace", so in my statement
> '#define line __LINE__' what I am saying is: the next time I use
> 'line' what I really mean is '__LINE__', but in my rush to finish the
> program I started to treat 'line' as a variable, i.e. std::cout <<
> jme::line << std::endl which of course is wrong, that is clear to me now.
>
> I have handed in the program at school, and even though it works, I
> still have a lot of questions about 'namespace'. I know what you are
> thinking, ask the teachers! that's what my dad tells me too. Well I
> have, but the teachers in my high school are too preoccupied with the
> Christmas stuff to explain it in way that makes sense to ME and not only
> to THEM. I mean. So, any help from you guys will be appreciated.
>
> Please don't be rude if I ask questions that are very simple to you, and
> above all, don't take me wrong if I say something that might sound
> insulting; it is never my intention to be rude to anyone.
> I share my email with my dad; he was really P.O. about some guy wring
> obscenities to me. I mean, think about what would you feel if someone
> starts using that kind of language with your kids, eh!
>
> Hava a good Ho Ho Ho
>
> This class is part of a program I wanted to hand in at school, but as we
> all know
> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
> =3D=3D=3D GCC =3D=3D=3D
> namespace jme {
>
> #define method __PRETTY_FUNCTION__
> #define file __FILE__
> #define line __LINE__
> #define function __func__
>
> enum error_t{
> //Generic error codes
> msg, //Default message
> warning, //Warning
> fatal, //Display fatal and exit
> .....
> };
> class Exception{
> protected:
> error_t error;
> std::string File, Method;
> size_t Line;
>
> public:
> //Setters
> void setException( jme::error_t, e // Error code
> const std::string&, fn // File name
> const std::string&, m // Method
> size_t l) // Line
> { error =3D e; File =3D fn; Method =3D m; Line =3D l; }
>
> };// Exception
> } // namespace
> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
> --- Someprogram.H -----
> namespace jme{
> class SomepogramEx : virtual public jme::Exception{
> public:
> SomeprogramEx(error_t ec, const std::string& f,
> const std::string& m, size_t l);
> };//End of SomeprogramEx
> class Someprogram{
> std::string* str_example;
> public:
> Someprogram(){
> try{str_example =3D new std::string;}
> catch(std::bad_alloc& x){
> // This works!!!!
> throw jme::SomeprogramEx(msg, file,
> method, line);
> // This doesn't ?????
> throw jme::SomeprogramEx(jme::msg,
> jme::file, jme::method, jme::line);
> }
> }
> }; //Someprogram
> } // namespace
>
> ce...@te... wrote:
>
> > Yeah, I understand that perfectly.
> > I understood the last maya's question as "passing them (__func__ and
> > __line__) to a srting!!!??? What you say passes the value, I wanted to
> > pass the variable name*". The answer, is that her code, was intending
> > to do just that an std:string variable to which a __func__ or
> > __liner__ were to be passed..
> > It was like her was thinking that when passing the #defined name for
> > them the item passed to her string variables where something like the
> > name of the "definition" variable, which has no sense. Hum, in that
> > moment I saw that sense of her question clearly, know, I wonder if I
> > really understood her question.
> >
> > And yes, the errors should appear there, but I've noticed mingw has
> > some tendency to point the errors related to #defined thing in the
> > next lines; and thos related to missing ';' in headers to the begining
> > of the source file which includes the header.
> >
> > Thanks.
> >
> > P.S. - The high cross-posting comes from her message, I simply clicked
> > 'reply all'
> >
> > *which translated to code, say
> > __line__ =3D "this is the line blah" (sorry if this has no sense, but
> > I'd never used this);
> > She thinks,
> > if #define foo __line__
> > std::string yuu =3D foo; ----> yuu =3D "__line__" (which makes no sense=
)
> > instead of "this is the line blah".
> >
> > Lars Hallberg wrote:
> >
> >> Carlos Garc=EDa del Monte wrote:
> >>
> >>> For what I see, your code is doing exactly the same, but without
> >>> using those #defines
> >>
> >>
> >>
> >>
> >> The strange thing is that You get the error in the std::cout. You
> >> suposed tho get them erlier.
> >>
> >> #define declare preprosessor constant ('dumb' text string constants)
> >> that is global, they shuld not care about scope. They are replaced
> >> litary as text. Your code:
> >>
> >> std::string f =3D foo::file;
> >> size_t l =3D foo::line;
> >>
> >> Transforme to:
> >>
> >> std::string f =3D foo::__func__;
> >> size_t l =3D foo::__LINE__;
> >>
> >> or rather, the value of thes 'special' constants at the #define. The
> >> compile shuld likely find errors there :-/
> >>
> >> Maya:s code actuly declare thes constants as 'real' constants in the
> >> scoop foo, shuld make all the differens!
> >>
> >> PS Litle *too* much crosposting this, is it not? DS
> >>
> >> HTH /LaH
> >>
> >>>
> >>> Maya wrote:
> >>>
> >>>> I want to replace the word __func__ with the word file, and you are
> >>>> telling me to assign the value of __func__ to the std::string
> >>>> variable?
> >>>
> >>>
> >>
> >
> >
> >
> > -------------------------------------------------------
> > This SF.net email is sponsored by: IBM Linux Tutorials.
> > Become an expert in LINUX or just sharpen your skills. Sign up for IBM=
's
> > Free Linux Tutorials. Learn everything from the bash shell to sys admi=
n.
> > Click now! http://ads.osdn.com/?ad_id=3D1278&alloc_id=3D3371&op=3Dclick
> > _______________________________________________
> > Dev-cpp-users mailing list
> > Dev...@li...
> > TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
>
> --
>
> FYI
> http://www.astro.umd.edu/~marshall/abbrev.html
>
> E-Mail Policy
> http://www.vif.com/users/escalante/Email_Policy.html
>
> * You cannot exercise your power to a point of humiliation.
> - Jean Chretien
>
> * The media's the most powerful entity on earth. They have the
> power to make the innocent guilty and to make the guilty
> innocent, and that's power.
> - Malcom X
> * "Innocent until proven guilty", no... not in Canada!!
>
>
>
>
> -------------------------------------------------------
> This SF.net email is sponsored by: IBM Linux Tutorials.
> Become an expert in LINUX or just sharpen your skills. Sign up for IBM's
> Free Linux Tutorials. Learn everything from the bash shell to sys admin.
> Click now! http://ads.osdn.com/?ad_id=3D1278&alloc_id=3D3371&op=3Dclick
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|
|
From: Maya <Esc...@ne...> - 2003-12-19 11:39:07
|
Thanks!
That's what I call knowing stuff, man!
One last question, in the case below, why is it that I get an error!
namespace jme{
enum error_t{
//Generic error codes
MSG, //Default message
WARNING, //Warning
FATAL, //Display fatal and exit
.....
};
class Someprogram{
std::string* str_example;
public:
Someprogram(){
try{str_example = new std::string;}
catch(std::bad_alloc& x){
// This works!!!!
throw jme::SomeprogramEx(MSG, FILE, METHOD,LINE);
// This doesn't ?????
throw jme::SomeprogramEx(jme::MSG, FILE, METHOD, LINE);
}
}
}; //Someprogram
} // namespace
By the way, Maya is the Sanskrit [ ancient Hindu language ] word for
illusion. It's a hippy thing... I guess. :-\
pw...@ia... wrote:
>1)
>Please. Take as a custom to always use ALL CAPS when #define symbols. It
>is practically impossible to read the code if you don't. Since a #defined
>symbol might be just about any type of expansion it is really important
>when reading the code.
>
>2)
>Since a #defined symbol is a preprocessor construct, they are never - I
>repeat NEVER - inside any namespace. If the compiler have seen the
>#define, and it hasn't been terminated by an #undefine, it will be used.
>The compiler complains, since you try jme::file, jme::line, ... where file
>and line are #defines.
>
>When you write jme::file it represents jme::__FILE__ which is expanded to
>jme::"filename" which isn't a valid construct.
>
>/Per W
>
>On Thu, 18 Dec 2003, Maya wrote:
>
>
>
--
FYI
http://www.astro.umd.edu/~marshall/abbrev.html
E-Mail Policy
http://www.vif.com/users/escalante/Email_Policy.html
* You cannot exercise your power to a point of humiliation.
- Jean Chretien
* The media's the most powerful entity on earth. They have the
power to make the innocent guilty and to make the guilty
innocent, and that's power.
- Malcom X
* "Innocent until proven guilty", no... not in Canada!!
|