# 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 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 on other sites
Conner McCloud    1135
Try waiting a second and asking again. I wrap my namespaces like you do, and occasionally intellisense doesn't work on the first try.

CM

##### Share on other sites
Spoonbender    1258
I've waited a week or so now... [wink]

##### 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 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 on other sites
dave    2187
You should wrap your member functions in a namespace, all together. This is only for reasons of clarity for maintenance programmers.

ace

##### Share on other sites
kSquared    1356
Hmm, odd. It seems to recognize it for me. Are you using C++ Express or the full Studio?

##### Share on other sites
Some people have suggested deleting the intellisense file so it'll rebuild itself in the past. I haven't tried it myself, but it's a possibility I suppose.

##### Share on other sites
joanusdmentia    1060
I second the deletion of the intellisense file. I've had a couple of issues like this myself and deleting it worked.

##### Share on other sites
Spoonbender    1258
I've tried deleting that, didn't help.
And I'm using the full Visual Studio.