Wheres the best location in cpp file or hpp file for a namespace declaration?

Hello been coding in C++ for 3 years using Notepad++ but still need some information so my question is wheres the best location for a namespace declaration?


For example :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//main.cpp 
#include <iostream>

namespace TestGroup
{
    bool
    One = false,
    Two = true,
    Three = false,
    Four = 1,
    Five = 0;
}

int main()
{
   std::cout << One << Two << Three << Four << Five << std::endl;
   std::cout << "Hello there" << std::endl;
   return 0;
}
Last edited on
I don't understand what you are trying to say with that faulty code.
How can you not understand what I've written?

Namespace declaration is located in the main.cpp file under the header for std libary "iostream" which looks like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//main.cpp 
#include <iostream>

//////////////////////////////////////
namespace TestGroup
{
    bool
    One = false,
    Two = true,
    Three = false,                          ///<------------------- HERE
    Four = 1,
    Five = 0;
}
////////////////////////////////////////
int main()
{
   std::cout << One << Two << Three << Four << Five << std::endl;
   std::cout << "Hello there" << std::endl;
   return 0;
}

When I mean location , I'm saying where in the main.cpp file does the Namespace declaration which looks like :

1
2
3
4
5
6
7
8
9
namespace TestGroup
{
    bool
    One = false,
    Two = true,
    Three = false,
    Four = 1,
    Five = 0;
}

goes in a cpp or hpp file.

Do you understand?
Last edited on
They must be in the header, having them in the source is optional:
1
2
3
4
5
6
//header.hpp
namespace Test
{
    void f();
    void g();
}
1
2
3
4
5
6
7
8
9
10
11
12
//source.cpp
#include "header.hpp"

namespace Test
{
    void f()
    {
    }
}
void Test::g()
{
}
Awesome Thank You! :D , Header files always mostly for namespace declarations thanks ;)
Topic archived. No new replies allowed.