Personal computing discussed

Moderators: renee, SecretSquirrel, just brew it!

 
Veritas
Gerbil First Class
Topic Author
Posts: 113
Joined: Wed Dec 26, 2001 7:00 pm
Location: Houston, TX
Contact:

C++ namespace question

Mon May 19, 2003 4:02 pm

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?
 
just brew it!
Administrator
Posts: 54500
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Mon May 19, 2003 4:38 pm

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;
}
 
Buub
Maximum Gerbil
Posts: 4969
Joined: Sat Nov 09, 2002 11:59 pm
Location: Seattle, WA
Contact:

Mon May 19, 2003 6:46 pm

But that causes compiler errors


What compiler? Looks like it doesn't properly implement current C++ standards.
 
Veritas
Gerbil First Class
Topic Author
Posts: 113
Joined: Wed Dec 26, 2001 7:00 pm
Location: Houston, TX
Contact:

Tue May 20, 2003 7:47 pm

I am using g++ 3.2 (really mingw -- which is gcc for windows)
 
just brew it!
Administrator
Posts: 54500
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Tue May 20, 2003 8:23 pm

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.
Last edited by just brew it! on Tue May 20, 2003 10:29 pm, edited 1 time in total.
 
pez-king
Gerbil Jedi
Posts: 1556
Joined: Sat Aug 31, 2002 9:45 pm
Location: Pennsylvania
Contact:

Tue May 20, 2003 10:25 pm

I see no reason there should be an error unless your compiler isnt working happy.

Try a different compiler maybe
 
Craig P.
Gerbil Team Leader
Posts: 285
Joined: Tue Dec 31, 2002 3:12 am
Location: South Bend, IN

Tue Jul 08, 2003 10:34 pm

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.
 
Buub
Maximum Gerbil
Posts: 4969
Joined: Sat Nov 09, 2002 11:59 pm
Location: Seattle, WA
Contact:

Thu Jul 10, 2003 4:24 pm

Oh by the way, the correct C++ standard variable to use is "bool", not the old MS macro "BOOL". :-)
 
Craig P.
Gerbil Team Leader
Posts: 285
Joined: Tue Dec 31, 2002 3:12 am
Location: South Bend, IN

Fri Jul 11, 2003 3:13 pm

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.
 
Veritas
Gerbil First Class
Topic Author
Posts: 113
Joined: Wed Dec 26, 2001 7:00 pm
Location: Houston, TX
Contact:

Thu Jul 17, 2003 10:46 am

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 ;^)
 
Craig P.
Gerbil Team Leader
Posts: 285
Joined: Tue Dec 31, 2002 3:12 am
Location: South Bend, IN

Thu Jul 17, 2003 11:20 am

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)

Who is online

Users browsing this forum: No registered users and 1 guest
GZIP: On