By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,655 Members | 1,005 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,655 IT Pros & Developers. It's quick & easy.

Why is this not allowed anymore (anymore being with a more recent compiler)?

P: n/a
So I tried to compile code that my boss wrote with a newer compiler
than I was previously using (FYI, the code compiles fine on g++ 3.4.4
but not on 4.1.1). I condensed it into this simple testprogram:

#include <string>

class Foo
{
private:
static const std::string LETTERS;
};

using namespace std;

namespace
{
const string Foo::LETTERS("abc");
}

int
main()
{
Foo f;

(void)f;
}

I must admit I'm not entirely sure what he want the anonymous (correct
term?) namespace for, but the more recent compiler sure doesn't like
it:
$ g++ --version
g++ (GCC) 4.1.1
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is
NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.

$ g++ -Wall -Wextra -std=c++98 -pedantic foo.cpp -o runme.exe
foo.cpp:13: error: definition of 'Foo::LETTERS' is not in namespace
enclosing 'Foo'

I need you gurus on this one. :-)

/ E

Nov 6 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
VJ
Eric Lilja wrote:
So I tried to compile code that my boss wrote with a newer compiler
than I was previously using (FYI, the code compiles fine on g++ 3.4.4
but not on 4.1.1). I condensed it into this simple testprogram:

#include <string>

class Foo
{
private:
static const std::string LETTERS;
};

using namespace std;

namespace
{
const string Foo::LETTERS("abc");
}

int
main()
{
Foo f;

(void)f;
}

I must admit I'm not entirely sure what he want the anonymous (correct
term?) namespace for, but the more recent compiler sure doesn't like
anonymous namespace is instead of using static variables
(variables/functions are only visible in the file where they are defined)
it:
$ g++ --version
g++ (GCC) 4.1.1
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is
NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.

$ g++ -Wall -Wextra -std=c++98 -pedantic foo.cpp -o runme.exe
foo.cpp:13: error: definition of 'Foo::LETTERS' is not in namespace
enclosing 'Foo'

I need you gurus on this one. :-)

/ E
Your program compiles for me without warnings and errors. My compiler is:
gcc version 4.0.2 20051125 (Red Hat 4.0.2-8)
Nov 6 '06 #2

P: n/a
* Eric Lilja:
So I tried to compile code that my boss wrote with a newer compiler
than I was previously using (FYI, the code compiles fine on g++ 3.4.4
but not on 4.1.1). I condensed it into this simple testprogram:

#include <string>

class Foo
{
private:
static const std::string LETTERS;
};

using namespace std;

namespace
{
const string Foo::LETTERS("abc");
}

int
main()
{
Foo f;

(void)f;
}

I must admit I'm not entirely sure what he want the anonymous (correct
term?) namespace for, but the more recent compiler sure doesn't like
it:
$ g++ --version
g++ (GCC) 4.1.1
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is
NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.

$ g++ -Wall -Wextra -std=c++98 -pedantic foo.cpp -o runme.exe
foo.cpp:13: error: definition of 'Foo::LETTERS' is not in namespace
enclosing 'Foo'

I need you gurus on this one. :-)
9.4.2/2 "The definition of a static data member shall appear in a
namespace scope enclosing the member's class definition" (the same rule
applies to member functions).

In the above code, the static data member is defined in a namespace that
does not enclose the class definition.

The anonymous namespace is just silly and incorrect the way it's used
above, but you could put the class definition inside. Also, in C and
C++ preferentially reserve all uppercase names for macros (is your boss
a former Java programmer?). Using all uppercase you (or your boss) have
no reason to complain if some header file defines a macro LETTERS that
interferes destructively with the code.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 6 '06 #3

P: n/a
"Eric Lilja" <mi********@gmail.comwrote:
So I tried to compile code that my boss wrote with a newer compiler
than I was previously using (FYI, the code compiles fine on g++ 3.4.4
but not on 4.1.1). I condensed it into this simple testprogram:

#include <string>

class Foo
{
private:
static const std::string LETTERS;
};

using namespace std;

namespace
{
const string Foo::LETTERS("abc");
}

int
main()
{
Foo f;

(void)f;
}

I must admit I'm not entirely sure what he want the anonymous (correct
term?) namespace for, but the more recent compiler sure doesn't like
it:
$ g++ --version
g++ (GCC) 4.1.1
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is
NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.

$ g++ -Wall -Wextra -std=c++98 -pedantic foo.cpp -o runme.exe
foo.cpp:13: error: definition of 'Foo::LETTERS' is not in namespace
enclosing 'Foo'

I need you gurus on this one. :-)
It doesn't look like it should compile. LETTERS is declared as a member
of Foo which is in global scope but never defined as such; instead it's
defined in anonymous namespace scope.

--
To send me email, put "sheltie" in the subject.
Nov 6 '06 #4

P: n/a
VJ
VJ wrote:
Eric Lilja wrote:
>class Foo
{
private:
static const std::string LETTERS;
};

using namespace std;

namespace
{
const string Foo::LETTERS("abc");
}
Reading other posts, I just realized that he put the definition of the
static variable inside anonymous namespace. If class Foo was declared in
another namespace, this would not compile at all
Nov 6 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.