   Trouble with namespaces...
  I''m trying to create a class with a predefined object (like cout, cerr, cin etc.) and then access it in the main function which contains another object of the same class and with the exact same name. Here''s some example code I threw together:main.cpp-----------------------------------------#include <iostream>#include "CObject.h"using namespace std;int main() {CObjectH::CObject cobject(); //Local object//Prints the order in which the objects were createdcout << "Pre-defined cobject: " << CObjectH::cobject.GetNum() << endl << endl;cout << "Local cobject: " << cobject.GetNum() << endl; //<-- It''s this line that generates the error.return 0;}CObject.h-----------------------------------------namespace CObjectH {#ifndef COBJECT_H#define COBJECT_Hclass CObject {public:CObject() { Num = cNum++; }; //Default constructorint GetNum();private:static unsigned long cNum;unsigned long Num;};extern CObject cobject;#endif}CObject.cpp-----------------------------------------------namespace CObjectH {#include "CObject.h"using namespace CObjectH;int CObject::GetNum() { return Num; }unsigned long CObject::cNum = 0;CObject cobject(); //Pre-defined object}  I get this error trying to compile this with VC6:\main.cpp(10) : error C2228: left of ''.GetNum'' must have class/struct/union typeI don''t get what it is I''m doing wrong so I''m hoping that someone here can tell me. Also why is it that I in CObject.cpp need to include "using namespace std;" to avoid writing CObjectH before every variable and function but I don''t (and can''t or I''ll get a whole bunch of errors) in CObject.h.Appreciate your help. 
  * The way you placed your includes, you have two nested namespace CObjectH. When included from main.cpp, CObject.h creates a ::CObjectH namespace, but when included from within ::CObjectH in CObject.cpp, it creates a ::CObjectH::CObjectH namespace. The way to fix this should be obvious.* You don''t have to use fully qualified names when you are within the namespace itself.* using namespace std; imports the std namespace into the global namespace. using namespace CObjectH; would do the same for your namespace.* CObject cobject(); is a function declaration - a function taking no parameters and returning a CObject - not an object definition.   [ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ][ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ] ;  An interesting note is that GCC 3.2 allows this:struct Foo {  void bar();}namespace Foo {  void bar() {    //do something  }}  I.e. you won''t have to type this to define a member function:void Foo::bar() {  //do something}  Well, unfortunately this is AFAIK non-standard behaviour.. At least it doesn''t work in VS.NET 7.1 ;  Thanks for your help! And I must say that''s probably the best answer I''ve ever gotten to a question. ;  Two rules that will help keep your namespaces out of trouble:1. Never put "#include ..." inside a namespace.2. Never put "using namespace ...;" in a header file. 