C++ namespace question

From Visual Basic to GNU C, this is the place to talk programming.

Moderators: SecretSquirrel, just brew it!

C++ namespace question

Postposted on 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:

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


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


But that causes compiler errors. Any clues?
Veritas
Gerbil First Class
 
Posts: 113
Joined: Wed Dec 26, 2001 7:00 pm
Location: Houston, TX

Postposted on 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++.

Code: Select all
#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):

Code: Select all
#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;
}
just brew it!
Administrator
Gold subscriber
 
 
Posts: 38088
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Postposted on Mon May 19, 2003 6:46 pm

But that causes compiler errors


What compiler? Looks like it doesn't properly implement current C++ standards.
Buub
Maximum Gerbil
Silver subscriber
 
 
Posts: 4214
Joined: Sat Nov 09, 2002 11:59 pm
Location: Seattle, WA

Postposted on Tue May 20, 2003 7:47 pm

I am using g++ 3.2 (really mingw -- which is gcc for windows)
Veritas
Gerbil First Class
 
Posts: 113
Joined: Wed Dec 26, 2001 7:00 pm
Location: Houston, TX

Postposted on 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.
just brew it!
Administrator
Gold subscriber
 
 
Posts: 38088
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Postposted on 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
pez-king
Gerbil Jedi
 
Posts: 1558
Joined: Sat Aug 31, 2002 9:45 pm
Location: Pennsylvania

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

Postposted on 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". :-)
Buub
Maximum Gerbil
Silver subscriber
 
 
Posts: 4214
Joined: Sat Nov 09, 2002 11:59 pm
Location: Seattle, WA

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

Postposted on Thu Jul 17, 2003 10:46 am

Oddly enough, it works fine doing this now as follows:

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


I did not change a thing...there must be a problem with VC++6.0 ;^)
Veritas
Gerbil First Class
 
Posts: 113
Joined: Wed Dec 26, 2001 7:00 pm
Location: Houston, TX

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


Return to Developer's Den

Who is online

Users browsing this forum: No registered users and 1 guest