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.
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:
#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.