Sign in to follow this  

namespaces, header files and variables

This topic is 4166 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hmmm.. a problematic trio. currently I have the following in a header file: namespace some_namespace{ extern int some_variable; } Then in the source file I have to (want to) initialize the variable: some_namespace::somevariable = 123; Is this the best way to do it???

Share this post


Link to post
Share on other sites

int some_namespace::somevariable = 123;

Though you can also do

namespace some_namespace {
int somevariable = 123;
}

Which is best depends on how much typing you want to do, and how many other items are in your namespace that you want to initialize.

Share this post


Link to post
Share on other sites
Hi,

Maybe it will help if you put a ifndef in you header.

Like :


//contents of some.h
#ifndef __SOMEH__
#define __SOMEH__

your initialization of the value and header contents

#endif


this ensures you don't initialize a value twice when including the header multiple times

Hope this helps,

Greetings...

Share this post


Link to post
Share on other sites
The code that SiCrane gave you:
namespace some_namespace {
int some_variable = 123;
}

should go in the source file. Have you put it in the header file?

To summarise, the header should contain
namespace some_namespace{
extern int some_variable;
}
and the source file either
int some_namespace::some_variable = 123;
or
namespace some_namespace {
int some_variable = 123;
}

Share this post


Link to post
Share on other sites

This topic is 4166 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this