Enclosing classes in namespaces

I am trying to figure out how to include a class in a namespace, but
I can't even get it to compile. Here is a toy class inside of a toy
namespace, x. Compiling with g++ -c base1.cpp yields three pages of
error messages which I won't repeat here. If the namespace statement is
taken out, it compiles fine. Can someone tell me how to get this simple
code to compile? Thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
namespace x {

#include <iostream>

using std::cin;
using std::cout;
using std::endl;

#include "base1.h"

//-----------------------------------------------------------------

base1::base1(int i)
{
   k = i;
}

//-----------------------------------------------------------------

void base1::m1()
{
   cout << "k = " << k << endl;
}

}


Here is base1.h

1
2
3
4
5
6
7
8
9
class base1 {
   public:
      base1(int i);

   private:
      void m1();

      int k;
};

base1.h
1
2
3
4
5
6
7
8
9
10
11
namespace x{
   class base1 {
      public:
         base1(int i);

      private:
         void m1();

         int k;
   };
}


base1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
#include "base1.h"
#include <iostream>
namespace x{
   base1::base1(int i)
   {
      k = i;
   }
}

//or
void x::base1::m1(){
	std::cout << "K= " << k << std::endl;
}

Topic archived. No new replies allowed.