🔒 Locked
Namespace Declaration
Advertisement
Original Post
Hey
I''m having some trouble declaring/defining a namespace. It''s rather simple, so I have everything in the header, i.e.
#pragma once
namespace MyNamespace
{
int Function() {return Variable;}
int Variable;
};
However, when I include the header twice, it tells me that I have multiply defined objects. Do I HAVE to define my namespace in a source file and declare it in a header (can''t remember exact term), ie
HEADER
#pragma once
namespace MyNamespace;
because I really hate doing that (hides the whole damn declaration, never mind implemention, if it''s even possible) or must I use some mixture of the two?
Thanks for your help.
Ronen D. Agranat
I don''t know much about namespaces though you could try doing this in your header file,
#ifndef _HEADERFILENAME_
#define _HEADERFILENAME_
/*
Insert your code here
*/
#endif
This should get rid of those multiply defined objects errors. In practice, I have found it useful to do this in ALL my header files (makes life a bit easier). Hope this helps,
BloatWare
#ifndef _HEADERFILENAME_
#define _HEADERFILENAME_
/*
Insert your code here
*/
#endif
This should get rid of those multiply defined objects errors. In practice, I have found it useful to do this in ALL my header files (makes life a bit easier). Hope this helps,
BloatWare
It has nothing to do with the namespace. You simply defined a function in a header file, which is a no-no. Either make the function inline, or put it in a source file (.cpp).
I tried that; doesn''t work. And why can''t you put functions in a header file? I have many functions my class definitions. Also, putting the definition in the declaration makes the function inline automatically. Plus, it gives me a multiple object error for the variable as well.
I''ll try putting the inline directive there anyway.
Ronen D. Agranat
I''ll try putting the inline directive there anyway.
Ronen D. Agranat
null''s semi-right. It''s actually fine to define functions and declare variables in the header file (which is what you are doing), as long as you only do it once. If you add include guards around your code, it will solve your multiple definitions error. Unfortuantely, you will now get a
"error C2065: ''Variable'' : undeclared identifier"
Your implementation of Function uses the global Variable before it''s declared. To fix it, reverse the lines.
I''m a little distrubed that you seem to be treating namespaces like classes. Be very careful. Namespaces (IMHO) are often more trouble than they''re worth, and I use them only to sort out collisions when integrating modules.
Here''s a working version of the program for you to try out:
"error C2065: ''Variable'' : undeclared identifier"
Your implementation of Function uses the global Variable before it''s declared. To fix it, reverse the lines.
I''m a little distrubed that you seem to be treating namespaces like classes. Be very careful. Namespaces (IMHO) are often more trouble than they''re worth, and I use them only to sort out collisions when integrating modules.
Here''s a working version of the program for you to try out:
|
quote:
Original post by The_Minister
I tried that; doesn''t work. And why can''t you put functions in a header file? I have many functions my class definitions. Also, putting the definition in the declaration makes the function inline automatically. Plus, it gives me a multiple object error for the variable as well.
You''ve answered your own question; only inline functions may be defined in a header file. Member functions defined in the class are implicitly inline, and that''s the only way you can define class member functions in a header file (except for templates, but class templates are not classes until they are instantiated).
Non-inline (outline, for language lawyers out there) functions cannot be defined in header files. You need to place a function declaration in the header file and a function definition in the .cpp file:
// in myfunction.h
void myfunction();
// in myfunction.cpp
void myfunction()
{
// TODO: do something
}
You cannot define non-const variables in the header file. If you could, each file that included the variable would get a separate copy of the variable, which is NOT what you want. You want multiple files to have access to the same variable. To do this, you need to use the extern keyword when declaring the variable in the header file, and define it normally in the source file:
// in myfunction.h
extern int variable;
// in myfunction.cpp
int variable;
Stoffel's semi-correct.
The_Minister's code already has #pragma once, which provides the inclusion guards in MSVC. You do NOT want to define variables and non-inline functions in the header file, as this program will cause you errors:
Edited by - null_pointer on April 10, 2001 2:16:25 PM
// test.h
#pragma once
namespace Space{
int Variable;
int Function () { return Variable; }
}
// test.cpp
#include "test.h"
#include "test.h" // no problemo because of include guards
int main ()
{
return Space::Function (); // always returns zero
}
// test2.cpp - another source file using the variable and function
#include "test.h"
quote:
Linker output
Linking...
test2.obj : error LNK2005: "int __cdecl Space::Function(void)" (?Function@Space@@YAHXZ) already defined in test.obj
test2.obj : error LNK2005: "int Space::Variable" (?Variable@Space@@3HA) already defined in test.obj
Debug/Header Files.exe : fatal error LNK1169: one or more multiply defined symbols found
Error executing link.exe.
Header Files.exe - 3 error(s), 0 warning(s)
Edited by - null_pointer on April 10, 2001 2:16:25 PM
Okay, I got it working with globals declared with extern in the header.. but check this:
Is that what you meant? It seems to work, but I find that amazing...
Ronen D. Agranat
|
Is that what you meant? It seems to work, but I find that amazing...
Ronen D. Agranat
Actually the header can be included by more than one cpp file if both the it writen like this
#ifndef _x_H_
#define _x_H_
#includes
namespace space
{
class x
{
public:
x();
};
}
namespace space
{
x::x()
{
blah
}
}
#endif
[code]
ie you have to put the definition in the namespace and put your end if after your code.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement