Re: just C++ in xcode
Re: just C++ in xcode
- Subject: Re: just C++ in xcode
- From: Joe Kurtz <email@hidden>
- Date: Thu, 29 Sep 2005 16:54:58 -0700 (PDT)
Okay everyone, we're gettin' plenty of mileage from
this. But there's just one thing I don't yet
understand. I was pretty simplistic in my example. My
code actually looks more like one of Andreas'
examples:
in foo1.h:
> #include <iostream>
>
> namespace n { int x; };
> void func();
in foo1.cpp:
> using namespace std;
> using namespace n;
>
> void func()
> {
> x = 10; // modfies n::x
> cout << x << endl; // prints n::x "10"
> }
>
in main.cpp
> #include foo1.h
> using namespace std;
> using namespace n;
>
> int main( int argc, char* argv[] )
>{
> #pragma unused( argc, argv )
> func();
> cout << x << endl; // prints n::x "10" ??
> }
This compiles and runs okay using "debug" build
configuration, but the output of this is:
10
0
which really does mean I have two variables x: one for
each TU(?). But that's just not what I want! I want
one variable n::x to be referenced by any code in any
file. The compiler requires me to include a definition
of n (which I do via #include foo1.h) in every module,
but each definition seems independent.
If I use the default build configuration, the linker
says:
/usr/bin/ld: multiple definitions of symbol n::x
--- Andreas Grosam <email@hidden> wrote:
>
> On 29.09.2005, at 20:19, Markian Hlynka wrote:
>
> >
> > Ah! I get it! You mean because
> > x=1;
> >
> > wasn't actually in a code block! Thus, it's an
> illegal definition.
> > Sorry, missed that!
> >
> > But why, in your second example below (of 3) is x
> = 10; assigning to
> > n::x and not the x in global namespace?
> >
> > secondly, in place of "int x = 1;", could I use
> "n::x = 1", ie just
> > before the function, or is that illegal?
>
> I'm sorry, the 2. example is not valid. The "using
> namespace n"
> directive introduces an ambiguity to name x, so that
> this example is
> wrong and will not compile. The corrected version is
>
> #include <iostream>
>
> namespace n { int x; };
>
> using namespace std;
> using namespace n;
>
> int x = 1; // declaration/definition of x in global
> space
>
> static void func()
> {
> n::x = 10; // requires qualifier n, due to
> ambiguity with ::x
> cout << ::x << endl; // prints ::x in global
> space: "1"
> cout << n::x << endl; // prints n::x "10"
> }
>
>
> Although there is an ambiguity in x, which requires
> to qualify x, the
> names cout and endl work nicely without qualifiers.
>
> Without ambiguity of name x:
>
> #include <iostream>
>
> namespace n { int x; };
>
> using namespace std;
> using namespace n;
>
> static void func()
> {
> x = 10; // modfies n::x
> cout << x << endl; // prints n::x "10"
> }
>
>
>
> Hope, that clarifies. More details in the "name
> lookup rules" stated in
> the standard.
> Again, I appologize for the invalid example. :-(
>
>
> The 3. example using qualified names is ok, though.
>
>
> Andreas
>
> > Markian
> >
> >
> >> This is just illegal code (as explained
> previously):
> >> #include <header.h>
> >> using namespace n;
> >> x = 1; // error: illegal definition without a
> type.
> >> // This is not a reference to n::x!
> >> // A relaxed compiler would probably introduce
> x of type int
> >> // in global space as if it were written:
> >> // int x = 1;
> >>
> >>
> >>
> >> Here it works as you expect:
>
> bummer: this one is not valid!!
> >>
> >> #include <iostream>
> >> #include <n.h>
> >>
> >> using namespace std;
> >> using namespace n;
> >> int x = 1; // declaration-definition of x in
> global space
> >> static void func()
> >> {
> >> x = 10; // that's an assignment to n::x
> >> cout << ::x << endl; // prints ::x "1"
> >> cout << x << endl; // prints n::x "10"
> >> }
> >>
> >>
>
> this one is ok
> >> without "using":
> >> #include <iostream>
> >> #include <n.h>
> >>
> >> int x = 1;
> >> void func();
> >> static void func()
> >> {
> >> n::x = 10;
> >> std::cout << ::x << std::endl; // prints ::x
> "1"
> >> std::cout << n::x << std::endl; // prints n::x
> "10"
> >> }
> >>
> >>
> >> Andreas
> >>
> >>
> >>
> >>> Chris
> >>>
> >>> On Thu, 29 Sep 2005 15:07:48 +0200, Andreas
> Grosam wrote
> >>> > On 29.09.2005, at 05:53, Marshall Clow wrote:
> >>> >
> >>> >
> >>>
> >>>> > Maybe this is a C++ question not having to do
> with
> >>>> > xcode, but I'm desparate...
> >>>> >
> >>>> > If I have a named namespace in foo1.h:
> >>>> ...
> >>>>
> >>> > namespace n {
> >>> > int x;
> >>> > };
> >>> > using namespace n;
> >>> > x = 1;
> >>> >
> >>> > The statement above
> >>> > x = 1;
> >>> > actually introduces a new variable in global
> space which does not
> >>> have a type.
> >>>
> >>>
> >> _______________________________________________
> >> Do not post admin requests to the list. They will
> be ignored.
> >> Xcode-users mailing list
> (email@hidden)
> >> Help/Unsubscribe/Update your Subscription:
> >>
> >> email@hidden
> >>
> >> This email sent to email@hidden
> >
> > ----
> > Quantum mechanics -- the dreams that stuff is made
> of.
> >
> >
> >
> > _______________________________________________
> Do not post admin requests to the list. They will be
> ignored.
> Xcode-users mailing list
> (email@hidden)
> Help/Unsubscribe/Update your Subscription:
>
>
> This email sent to email@hidden
______________________________________________________
Yahoo! for Good
Donate to the Hurricane Katrina relief effort.
http://store.yahoo.com/redcross-donate3/
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden