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 Very Happy

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.
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 Very Happy
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.

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?

c++:

#include <allegro.h>
#include <sstream>
using namespace std;
namespace Jobs {
/* Declaring My Variables */
// INTEGERS
int pointsLeft = 10;
int numY = 0;
int numX = 0;
int num = 0;
int pointsGain [6] = {0,0,0,0,0,0};
int AtkVal;
int DefenseVal;
int Sequence = 0;
int JobNumber = 0;
int setJobNumber;
int totalPoints = 0;
int maxPoints =4;

// BOOLEAN
bool Gain = true;
// BITMAPS
BITMAP *bBackgrounds [4];
BITMAP *bSelection [4];

// FONTS
FONT *MainFont;

/* End of Declaring My Variables */
 

/* Declaring and defining of my Functions */


bool JobStart (int sequence) { // or if designdone = true
if (sequence == 3)
return true;
else
return false;
}

void LoadImages () {
for (int i = 0; i < 4; i++) {
stringstream str;
str << "Jobs\\Background" << i + 1 << ".bmp";
bBackgrounds [i] = load_bitmap (str.str().c_str(), NULL);
if (!bBackgrounds [i])
allegro_message ("Error could not find %s" , str.str().c_str() );
} // End of For Loop (Backgrounds)

// i can put them both in the same for loop but I'm doing this for neatness purposes
for (int i = 0; i < 4; i++) {
  stringstream str;
  str << "Jobs\\Selection" << i+ 1 << ".bmp";
  bSelection [i] = load_bitmap (str.str().c_str(), NULL);
  if (!bSelection [i])
    allegro_message ("Error could not fine %s" , str.str().c_str() );
  }

 MainFont = load_font ("Fonts.pcx", NULL, NULL);
} // End of LoadImages

void Display (BITMAP *Buffer) {
blit (bBackgrounds [JobNumber], Buffer , 0, 0, 0, 0, 640, 480);
masked_blit (bSelection [JobNumber], Buffer, 0, 0, 0, 0, 640, 480);
textout_ex (Buffer, MainFont, "Warrior", 520, 45, makecol (255, 0, 0), -1);
textout_ex (Buffer, MainFont, "Mage", 520, 165, makecol (255, 0, 0), -1);
textout_ex (Buffer, MainFont, "Thief", 520, 285, makecol (255, 0, 0), -1);
textout_ex (Buffer, MainFont, "Knight", 520, 405, makecol (255, 0, 0), -1);
} // End of Display
void ChooseJob () { // choose class
if (Sequence == 0) {
} // End of If Sequence == 0
} // End of ChooseJob

void Controls () {
if (Sequence == 0) {
if (key [KEY_UP])
JobNumber --;
else if (key [KEY_DOWN])
JobNumber ++;
rest (40);
} // End of If Sequence == 0
else if (Sequence == 1) {
while (Sequence == 1)
if (key [KEY_DOWN]) {
numY += 100;
num ++;
} // End of If statement
else if (key [KEY_UP])  {
numY -= 100;
num --;
} // End if Else-If Statement
if (key [KEY_RIGHT]) {
pointsLeft --; // you lose points when you add them to an attribute.
if (Gain == true)
pointsGain[num] ++;
}// End if If Statement
else if (key [KEY_LEFT]){
pointsLeft ++;
pointsGain[num] --; 
} // End if Else-If Statement
if (key [KEY_Z] && Gain == false) {
Sequence ++;
} // End of If Statement
} // End of Else-If Sequence == 1
} // End of Controls

void Restrictions () {
for ( int i = 0 ; i < 6 ; i ++ ) {
totalPoints += pointsGain [i];
} // End of For Loop
if (totalPoints >= maxPoints) {
Gain = false;
} else {
Gain = true;
} // End of Else Statement
if (JobNumber > 3)
JobNumber = 3;
else if (JobNumber < 0)
JobNumber = 0;
} // End of Restrictions

void Attack (BITMAP *Buffer) {
} // End of Attack

/* End of Declaring and Defining of my Functions */
}// End of namespace Jobs


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
#pragma once so I won't get errors from duplication
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.

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:Mad += 5;
}

ERROR - test.obj : error LNK2005: "int test:Mad" (?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
code:
[syntax="cpp"][/syntax]
tags for your code.

c++:
// test.h
namespace test {
   int x = 10;
   void changex ();
}

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:
c++:
   extern int x;
and put the definition in an implementation file.


: