CodeBlocks namespace problem

Hello, I have been learning namespaces in classes using CodeBlocks IDE and I have encountered a problem. Whenever I want to create 2 classes with identical names and separate them using namespaces, the first namespace works perfectly but when I try to use the second one it says that "the "namespace name" does not name a type". I would appreciate help. Thanks.

P.S. I have headers and cpp's in separate files, but i will paste code as one here.


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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Main file  

#include <iostream>
#include "lesson.h"
#include "lesson_t.h"

int main()
{
    one::lesson a;
    one::a.print();


    two::lesson a;

    two::a.print();

    return 0;
}

// End of main

// Start of "lesson.h header"

#ifndef LESSON_H
#define LESSON_H


namespace one{

class lesson
{
    public:
        lesson();
        virtual ~lesson();
        void print();

    protected:

    private:
};

}

#endif // LESSON_H


// End of "lesson.h header"


//Start of "lesson.cpp"

#include "lesson.h"
#include <iostream>

namespace one{

lesson::lesson()
{
    //ctor
}

lesson::~lesson()
{
    //dtor
}

void lesson::print(){

    std::cout<<"Text1"<<std::endl;
}

}


//End of "lesson.cpp"

//Start of "lesson_t.h" header

#ifndef LESSON_H
#define LESSON_H

namespace two {

class lesson
{
    public:
        lesson();
        virtual ~lesson();
        void print();

    protected:

    private:
};

}

#endif // LESSON_H


//End of "lesson_t.h" header

//Start of "lesson_t.cpp"

#include "lesson_t.h"
#include <iostream>

namespace two {

lesson::lesson()
{
    //ctor
}

lesson::~lesson()
{
    //dtor
}

void lesson::print(){

std::cout<<"Text2"<<std::endl;

}

}

//End of lesson_t.cpp"
Hello JustinPlusPlus,

This is the first thing that jumps out at me when I look at your code:
1
2
3
4
//Start of "lesson_t.h" header

#ifndef LESSON_H  << --- Already defined in "lesson.h" file. Try using "LESSON2_H".
#define LESSON_H 

When line 3 is reached the "#ifndef" will see that "LESON_H" has already been defined and skip this bit of code.

I will load this up and see what else I can find.

Hope that helps for now,

Andy
Hello JustinPlusPlus,

I loaded up the program in VS and as I thought using the same name for the include guards is a problem. When I looked at the "lesson_t" header file it was grayed out because "LESSON_H" was already defined. When I changed the name it worked fine.

The next problem is a simple one in main. The comments should explain it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <limits>  // <--- Added.
#include "lesson.h"
#include "lesson_t.h"

int main()
{
	one::lesson a;  // <--- This is OK
	a.print();  // <--- a is already defined as being in the "one" name space and its type is lesson. The qualifier is not needed.

	two::lesson b;  // <--- Redefinition of "a" needs to be a different variable name.
	b.print();

	// This next line may not be needed. If you have to press Enter to see the prompt then comment out the
	// next line. If not you will need this to clear the input buffer first.
	//std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires heder file <limits>.
	std::cout << "\n\n Press Enter to continue";
	std::cin.get();

	return 0;
}

The lines just before return are need by some to keep the console window open at the end of the program.

Hope that helps,

Andy
Topic archived. No new replies allowed.