Page 1 of 1

C++ namespace question

Posted: Mon May 19, 2003 4:02 pm
by Veritas
OK, I am finally forcing myself to start implementing namespaces in my projects. I have a question as how to make the following a little more elegent:

BOOL GL_Window::Initialize ()
{
                ...
   {
   using namespace glGraph;
   g_graph = new CglGraph();
   }
                ...
}


I try doing:
 g_graph = new glGraph::CglGraph();


But that causes compiler errors. Any clues?

Posted: Mon May 19, 2003 4:38 pm
by just brew it!
What sort of errors are you getting? The following test program compiles and runs just fine, both under Visual C++ and g++.

#include <stdio.h>

namespace ns1
{
    class c1
    {
    public:
        c1() { printf("c1()\n"); };
        ~c1() { printf("~c1()\n"); };
    };
    typedef c1 *c1ptr;
};

int main(int argc, char* argv[])
{
    printf("enter main()\n");
    ns1::c1ptr p;
    {
        p = new ns1::c1();
        delete p;
    }
    printf("exit main()\n");
   return 0;
}


Edit: Another version, probably closer to what you are doing (one namespace invoking another):

#include <stdio.h>

namespace ns1
{
    class c1
    {
    public:
        c1() { printf("c1()\n"); };
        ~c1() { printf("~c1()\n"); };
    };
    typedef c1 *c1ptr;
};

namespace ns2
{
    class c2
    {
    public:
        c2() { printf("c2()\n"); };
        ~c2() { printf("~c2()\n"); };
        void Initialize();
        ns1::c1ptr p1;
    };
    typedef c2 *c2ptr;
};


void ns2::c2::Initialize()
{
    printf("c2::Initialize()\n");
    p1 = new ns1::c1();
    delete p1;
}


int main(int argc, char* argv[])
{
    printf("enter main()\n");
    ns2::c2ptr p2;
    {
        p2 = new ns2::c2();
        p2->Initialize();
        delete p2;
    }
    printf("exit main()\n");
   return 0;
}

Posted: Mon May 19, 2003 6:46 pm
by Buub
But that causes compiler errors


What compiler? Looks like it doesn't properly implement current C++ standards.

Posted: Tue May 20, 2003 7:47 pm
by Veritas
I am using g++ 3.2 (really mingw -- which is gcc for windows)

Posted: Tue May 20, 2003 8:23 pm
by just brew it!
I am still unsure of what your problem is. How does your code differ structurally from the example I posted above? And what error message are you getting?

Edit: FWIW, I used g++ 3.2 to compile my test code.

Posted: Tue May 20, 2003 10:25 pm
by pez-king
I see no reason there should be an error unless your compiler isnt working happy.

Try a different compiler maybe

Posted: Tue Jul 08, 2003 10:34 pm
by Craig P.
If you can produce a stripped-down example that still doesn't compile, the online compiler at http://www.comeaucomputing.com is a good sanity check. It's generally considered to be the most standard-conforming in existence, and there are only a few corner cases where there's any dispute as to whether it does the right thing.

Posted: Thu Jul 10, 2003 4:24 pm
by Buub
Oh by the way, the correct C++ standard variable to use is "bool", not the old MS macro "BOOL". :-)

Posted: Fri Jul 11, 2003 3:13 pm
by Craig P.
Buub wrote:
Oh by the way, the correct C++ standard variable to use is "bool", not the old MS macro "BOOL". :-)


Not if you're working with Windows headers or frameworks based on that (notably MFC, ATL and WTL may do it too).

In general, I would expect BOOL to be an integer typedef taking values TRUE and FALSE, but I think with Windows you sometimes get other values too. This is perfectly standard C++, if not necessarily the optimum way of doing it.

Posted: Thu Jul 17, 2003 10:46 am
by Veritas
Oddly enough, it works fine doing this now as follows:

BOOL GL_Window::Initialize () 
{
                ...
   g_graph = new glGraph::CglGraph();
                   ...
}


I did not change a thing...there must be a problem with VC++6.0 ;^)

Posted: Thu Jul 17, 2003 11:20 am
by Craig P.
Veritas wrote:
there must be a problem with VC++6.0 ;^)


Now that's the understatement of the year. ;)


(said somewhat for effect -- it's got holes, but it's still not that bad)