• Advertisement
Sign in to follow this  

namespace float redefinition

This topic is 1845 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

So I've been messing around with namespaces,here's what I have:

 

1.h

namespace x{
void d();

float j;
int m();

}

 

 

When i compile that I get this:

'float x::j' : redefinition

 

I don't understand,what am I doing wrong?!

Share this post


Link to post
Share on other sites
Advertisement

Looks like the header is getting included more than once in a file. You need include guards or #pragma once

 

Include guards look like this:

 

#ifndef INCLUDED_1_H

#define INCLUDED_1_H

 

// contents of header file

 

#endif

 

Each header file needs a uniquely named #define when doing it the include guards way.

Share this post


Link to post
Share on other sites

damn! you are right ...can't believe i didn't notice this.Thanks a lot!

Share this post


Link to post
Share on other sites

Also, the reason it didn't moan about d() or m() being redefined is because function declarations are implicitly extern (unless static). You could get away with no include guard (not recommended though) if you had

 

extern float j;

 

in the header and defined it as

 

float x::j;

 

in one and only one cpp file (presumably, 1.cpp).

 

And I hope the file name and variable names are just for illustration, otherwise, you're fired ;)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement