Jump to content
  • Advertisement
Sign in to follow this  
squashed_bug

namespaces, header files and variables

This topic is 4447 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
Advertisement

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
Thats exactly what I wanted to do. However the variable ends up being defined each time the header file is included (the namespace is in a header file), which causes link errors.

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!