0

Consider I have the following function prototype:

void MyFunction(int MyParameter);

With the following definition:

void MyFunction(int MyParameter)
{
    // Do stuff here.
}

Where should they each be put if I have a header file (no main function) with a namespace? Does the prototype go in the namespace and the definition outside it? Or do they both go in?

Maxpm
  • 24,113
  • 33
  • 111
  • 170

3 Answers3

2

If you choose to have a namespace, both should be inside :

.h :

namespace MyNameSpace {
void MyFunction(int MyParameter);
}

.cpp :

void MyNameSpace::MyFunction(int MyParameter)
{
    // Do stuff here.
}
Xavier V.
  • 6,068
  • 6
  • 30
  • 35
  • On an unrelated sidenote, why does the second file have to be a .cpp? – Maxpm Dec 06 '10 at 15:50
  • It does not have to be ".cpp". It must be something recognizable by your C++ compiler. Like .c, .cpp or .cxx for examples – Xavier V. Dec 06 '10 at 15:52
  • Is that just for debugging purposes (seeing if there are any syntax errors), or is there another reason? – Maxpm Dec 06 '10 at 15:54
  • 1
    You can have a look at http://stackoverflow.com/questions/1545080/correct-c-code-file-extension-cc-vs-cpp for more informations – Xavier V. Dec 06 '10 at 15:56
1

If your prototype is not in the namespace, then you do not have to put the definition in the namespace. If the prototype is in a namespace, the definition should be in the same namespace.

wkl
  • 77,184
  • 16
  • 165
  • 176
0

They both have to be in the namespace

ognian
  • 11,451
  • 4
  • 35
  • 33