Sign in to follow this  
Spoonbender

C++ namespaces and VS.Net

Recommended Posts

Spoonbender    1258
I'm a bit unsure on how you're "supposed" to work with namespaces in C++. Say, you have a headerfile declaring a class inside a namespace, something like this:
namespace foo {
  class bar {
    void somefunction();
    void someotherfunction();
  }

}

So far so good. But is there a "proper" way to write the corresponding cpp file? So far, I've wrapped those into namespace foo { } as well:
namespace foo {
  void bar::somefunction(){
  }
  void bar::someotherfunction(){
  }
}

But Visual Studio 2k5's Intellisense can't figure that out. It doesn't recognise class members. (Still compiles ok though) So that made me wonder, am I doing it "wrong"? I can think of two alternatives. Either specify the full namespace on every function in the cpp file, like this:
void foo::bar::somefunction(){
}

But that seems a bit verbose. Doesn't exactly help with code readability. Or alternatively, just put a "using namespace foo" at the top of the cpp file. But that doesn't seem a very elegant solution? I thought blanket "using namespace ..." statements were discouraged? Anyone know what the "proper" way to do this is?

Share this post


Link to post
Share on other sites
SiCrane    11839
I wrap the function definitions in the namespace. Just because intellisense doesn't like it too much doesn't mean it isn't valid.

Share this post


Link to post
Share on other sites
Conner McCloud    1135
Yes, but did you do so without messing with the cursor? The current namespace doesn't update as quickly as one might like, and until it figures out what namespace the cursor is currently in, it is going to give you bad results. Watch the bar at the top that shows what namespace you're in...it should update as you move the cursor around, and once it does intellisense should work properly. At least, it does for me.

CM

Share this post


Link to post
Share on other sites
Spoonbender    1258
Yep, I've tried that. It doesn't just take time, it shows a small message down in the statusbar at the bottom, saying something like "Right side of . is not a struct/class. Please see "Troubleshooting Intellisense in C++ projects"

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this