Computer Science Canada Quick C++ NameSpaces Question |
Author: | DrummaBoyFB [ Tue Dec 14, 2010 5:10 pm ] |
Post subject: | Quick C++ NameSpaces Question |
well I want to create a namespace and I've learned a bit about them. Now I don't want to create the namespace in my main.cpp file as it is to larger and I want my main.cpp to be short and neat. So should I create a header file and then create the namespace with all the values and what not OR should I do it the same way I do it with classes, declare the the variables in the header file and then put values in them in the .cpp file. So to sum it all up. Should I create the namespace in the header file alone or should I create it with a header file and .cpp file Thanks in Advance |
Author: | OneOffDriveByPoster [ Wed Dec 15, 2010 2:07 pm ] |
Post subject: | Re: Quick C++ NameSpaces Question |
DrummaBoyFB @ Tue Dec 14, 2010 5:10 pm wrote: well I want to create a namespace and I've learned a bit about them.
Unlike classes, namespaces in C++ are not subject to the one-definition rule by themselves. You can declare members of a namespace in multiple header files and the set of members declared in each do not have to be the same. Members of a namespace work just like normal, you should have class and inline function definitions in the headers and other definitions in a .cpp.Now I don't want to create the namespace in my main.cpp file as it is to larger and I want my main.cpp to be short and neat. So should I create a header file and then create the namespace with all the values and what not OR should I do it the same way I do it with classes, declare the the variables in the header file and then put values in them in the .cpp file. So to sum it all up. Should I create the namespace in the header file alone or should I create it with a header file and .cpp file Thanks in Advance |
Author: | DrummaBoyFB [ Wed Dec 15, 2010 4:51 pm ] |
Post subject: | Re: Quick C++ NameSpaces Question |
so basically what you're saying to do is to declare everything in a header file rather than have a header file and an implementation (.cpp) file ? |
Author: | OneOffDriveByPoster [ Wed Dec 15, 2010 5:05 pm ] |
Post subject: | Re: Quick C++ NameSpaces Question |
DrummaBoyFB @ Wed Dec 15, 2010 4:51 pm wrote: so basically what you're saying to do is to declare everything in a header file rather than have a header file and an implementation (.cpp) file ? No, I am saying you can declare whatever is in your interface and the inline function definitions in a header file and have an implementation file for your other definitions. A way for you to check is if your linker complains if you include your header in more than source file. |
Author: | DrummaBoyFB [ Wed Dec 15, 2010 6:01 pm ] |
Post subject: | Re: Quick C++ NameSpaces Question |
ohhh ok, so would it matter if i just did everything in a header file? or is that just completly bad programming practice |
Author: | DrummaBoyFB [ Wed Dec 15, 2010 6:14 pm ] | ||
Post subject: | Re: Quick C++ NameSpaces Question | ||
this is the code I have right now... would this be completely incorrect?
|
Author: | wtd [ Wed Dec 15, 2010 6:45 pm ] |
Post subject: | RE:Quick C++ NameSpaces Question |
No idea. Please indent your code. |
Author: | DrummaBoyFB [ Wed Dec 15, 2010 11:15 pm ] |
Post subject: | Re: Quick C++ NameSpaces Question |
Sorry. I normally indent but this was rushed. I mean was the format correct. Putting the namespace in a header file and declaring all the values it in. I just want to know if I could do that or would that be completely wrong |
Author: | wtd [ Thu Dec 16, 2010 12:16 am ] |
Post subject: | RE:Quick C++ NameSpaces Question |
It'll work. Nothing says you can't have (implementation) code in a header file. Just not a good idea. |
Author: | DrummaBoyFB [ Thu Dec 16, 2010 1:31 am ] |
Post subject: | Re: Quick C++ NameSpaces Question |
Alright. So can you explain to me what are the negative effects of not putting it in an implementation file? Is it just the way it's supposed to be done or is there a specific reason? |
Author: | OneOffDriveByPoster [ Thu Dec 16, 2010 6:54 pm ] |
Post subject: | Re: Quick C++ NameSpaces Question |
DrummaBoyFB @ Thu Dec 16, 2010 1:31 am wrote: Alright. So can you explain to me what are the negative effects of not putting it in an implementation file? Is it just the way it's supposed to be done or is there a specific reason? There is a specific reason. A (non-inline) function definition or the definition of a variable with static storage duration should only occur once for the same entity. So, if such an entity has external linkage and you provide your definition in a header file, then if you do not otherwise use macros to ensure that there is only on definition in your whole program, your program will be ill-formed.
The most likely symptom is a linker message indicating multiple definitions of the same symbol. |
Author: | DrummaBoyFB [ Fri Dec 17, 2010 10:21 am ] |
Post subject: | Re: Quick C++ NameSpaces Question |
Ohh Ok. Well I already designed it with just the header file so what I'll do is use #pragma once so I won't get errors from duplication |
Author: | OneOffDriveByPoster [ Fri Dec 17, 2010 2:30 pm ] |
Post subject: | Re: Quick C++ NameSpaces Question |
DrummaBoyFB @ Fri Dec 17, 2010 10:21 am wrote: so what I'll do is use
Your code will be less portable if it relies on #pragma once. Also, the basic use of #pragma once is to act as a #include guard. I am not sure how effective it is on preventing multiple symbol definition between separate object files.#pragma once so I won't get errors from duplication |
Author: | DrummaBoyFB [ Wed Dec 22, 2010 11:29 am ] |
Post subject: | Re: Quick C++ NameSpaces Question |
oh ok. But can I ask you something? Whenever I create a variable in my namespace Ex: // test.h namespace { int x = 10; void changex (); } When I creating my implementation file I can't change the value of x. Is there any way of doing so? Ex: // test.cpp #include "test.h" void test::changeX () { test: += 5; } ERROR - test.obj : error LNK2005: "int test:" (?x@test@@3HA) already defined in Main.obj what can I do to fix this? |
Author: | OneOffDriveByPoster [ Wed Dec 22, 2010 4:20 pm ] | ||||||
Post subject: | Re: Quick C++ NameSpaces Question | ||||||
FYI: Use
This is the problem; that's a definition of x that you have there. If you include this in more than one implementation file, you will get the link error above. Have a declaration instead:
|