namespace float redefinition

Started by
2 comments, last by Paradigm Shifter 11 years ago

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?!

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.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

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

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 ;)

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

This topic is closed to new replies.

Advertisement