help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Assigning values to namespace members


From: Bernd Strieder
Subject: Re: Assigning values to namespace members
Date: Fri, 08 Feb 2008 15:02:55 +0100
User-agent: KNode/0.10.4

Hello,

Peter Oberberg wrote:

> Maybe I did not understand something fundamental about namespaces,
> because I cannot make out the following behaviour.

This is a C++ question, but we are in a g++ newsgroup here. What could
be discussed is the error message of g++, which is not really helpful.
I have never seen that message before.

error: expected constructor, destructor, or type conversion before ?=?
token

This might be an artifact of the parser, it tries some cases, some
unfamiliar case as the last one which has to match, and that is the
message one gets. I don't know what the compiler tried last, where this
message could match.

The problem in the code is not difficult to see.

> 
> Please consider the following code snippet (it is a simplified version
> of what I want to do, in order to reproduce the error, actually the
> namespace declaration is in a separate header file).
> 
> // ==================================================
> // Begin C++ code, file TTest.cc
> 
> namespace TTest
> {

This is a definition, without initializer.

>   int value;
> };
> 

This is an assignment outside of a function, which is not possible:

> TTest::value = 1;
> 
> // End C++ code
> // ==================================================
> 

This works:

// ==================================================
// Begin C++ code, file TTest.cc

namespace TTest
{
    extern int value; // declaration
};

int TTest::value = 1; // definition with initializer, needs type

int main()
{
TTest::value = 1; // assignment expression in a statement
}

// End C++ code
// ==================================================




> So where am I wrong in thinking, that TTest::value is an entity, I can
> assign values to outside the namespace?

There can only be one definition of TTest::value, maybe with
initializers, and assignments may only happen inside of a function.

Bernd Strieder



reply via email to

[Prev in Thread] Current Thread [Next in Thread]