Topic: Namespaces and storage class


Author: ncm@nospam.cantrip.org (Nathan Myers)
Date: 1998/09/21
Raw View
Walter S. White<whitews@open.org> wrote:
>Martin Lang wrote in message <35E542DF.B737C9ED@alcatel.de>...
>>I tried to declare a variable inside a namespace but have
>>problems with the storage class.
>
>>In file dummy.h I wrote:
>>  namespace ns { extern int x; }
>>In dummy.cpp I have:
>>  #include "dummy.h"
>>  int ns::x;
>>And finally, dumm_main.cpp contains:
>>  #include "dummy.h"
>>  int main() { ns::x=3; }
>
>The keyword, "extern," should be deleted from the file, "dummy.h."
>
>The basic problem is that you simply don't need the file, "dummy.cpp."
>All it does is provide a redundant definition of "int ns::x" which has
>already been defined (and declared) in "dummy.h."
>
>Get rid of the keyword, "extern," in "dummy.h" and get rid of "dummy.cpp"
>altogether. You'll be home free. . . .

Walter is incorrect.  The program (as corrected above -- note the return
type for main() ) is correct; a compiler that doesn't accept it is broken.
In particular, if Martin followed Walter's advice, the "dummy.h" file
could not be #included in any other source file, which is not what is
wanted.

--
Nathan Myers
ncm@nospam.cantrip.org  http://www.cantrip.org/
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "Walter S. White" <whitews@open.org>
Date: 1998/09/17
Raw View
Your comments from the draft standard make sense. The keyword, "extern,"
should be deleted from the file, "dummy.h."

The basic problem is that you simply don't need the file, "dummy.cpp."
All it does is provide a redundant definition of "int ns::x" which has
already been defined (and declared) in "dummy.h."

Get rid of the keyword, "extern," in "dummy.h" and get rid of "dummy.cpp"
altogether. You'll be home free. . . .

Martin Lang wrote in message <35E542DF.B737C9ED@alcatel.de>...

>it tried to declare a variable inside a namespace but have
>problems with the storage class.

>In file dummy.h I wrote:

>namespace ns {
> extern int x;
>}

>In dummy.cpp I have:

>#include "dummy.h"
>int ns::x;

>And finally, dumm_main.cpp contains:

>#include "dummy.h"
>
>main()
>{
>   ns::x=3D1;
>   return 0;
>}

>When I link the objects I get an unresolved external for ns::x.
>If I remove the external from the declaration, I get a doubly definded
>symbol.
>Chapter 7.1.1 par. 6 of the Draft Standard says "A name declared in a
>namespace scope without a storage-class-specifier has external linkage
>unless it has internal linkage because of a previous declaration"

>My interpretation of this is that the external is not needed.
>Is this a compiler bug or am I doing something wrong. How should
>a correct declartion of a variable in a namespace look like, if I
>want to use it in several compilation unit, but have only one instance.

  {16 null-content lines trimmed... please don't overquote! -mod}

      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: ncm@nospam.cantrip.org (Nathan Myers)
Date: 1998/08/28
Raw View
Martin Lang <Martin.Lang@alcatel.de> wrote:
>I tried to declare a variable inside a namespace but have
>problems with the storage class.
>
> namespace ns { extern int x;}
> int ns::x;
> main() { ns::x=1; return 0; }
>
>When I link the objects I get an unresolved external for ns::x.
>
>My interpretation of this is that the external is not needed.
>Is this a compiler bug or am I doing something wrong.

You don't say what buggy compiler is reporting this "error".
Anyway, the code is fine, and means what you thought it should
mean.

--
Nathan Myers
ncm@nospam.cantrip.org  http://www.cantrip.org/



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Martin von Loewis <loewis@informatik.hu-berlin.de>
Date: 1998/09/02
Raw View
Martin Lang <Martin.Lang@alcatel.de> writes:

> Is this a compiler bug or am I doing something wrong.

Given your example, it seems that this is a compiler bug.
I don't think there could be a reasonable work-around.

Regards,
Martin


      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Martin Lang <Martin.Lang@alcatel.de>
Date: 1998/08/28
Raw View
Hi,

it tried to declare a variable inside a namespace but have
problems with the storage class.

In file dummy.h I wrote:

namespace ns {
 extern int x;
}

In dummy.cpp I have:

#include "dummy.h"
int ns::x;

And finally, dumm_main.cpp contains:

#include "dummy.h"

main()
{
   ns::x=1;
   return 0;
}

When I link the objects I get an unresolved external for ns::x.
If I remove the external from the declaration, I get a doubly definded
symbol.
Chapter 7.1.1 par. 6 of the Draft Standard says "A name declared in a
namespace scope without a storage-class-specifier has external linkage
unless it has internal linkage because of a previous declaration"

My interpretation of this is that the external is not needed.
Is this a compiler bug or am I doing something wrong. How should
a correct declartion of a variable in a namespace look like, if I
want to use it in several compilation unit, but have only one instance.

Martin


      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]