C++ namespaces and VS.Net

Started by
9 comments, last by Spoonbender 18 years, 4 months ago
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?
Advertisement
I wrap the function definitions in the namespace. Just because intellisense doesn't like it too much doesn't mean it isn't valid.
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
I've waited a week or so now... [wink]
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
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"
You should wrap your member functions in a namespace, all together. This is only for reasons of clarity for maintenance programmers.

ace
Hmm, odd. It seems to recognize it for me. Are you using C++ Express or the full Studio?
- k2"Choose a job you love, and you'll never have to work a day in your life." — Confucius"Logic will get you from A to B. Imagination will get you everywhere." — Albert Einstein"Money is the most egalitarian force in society. It confers power on whoever holds it." — Roger Starr{General Programming Forum FAQ} | {Blog/Journal} | {[email=kkaitan at gmail dot com]e-mail me[/email]} | {excellent webhosting}
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.
I second the deletion of the intellisense file. I've had a couple of issues like this myself and deleting it worked.
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The only verdict is vengeance; a vendetta held as a votive, not in vain, for the value and veracity of such shall one day vindicate the vigilant and the virtuous. Verily, this vichyssoise of verbiage veers most verbose, so let me simply add that it's my very good honor to meet you and you may call me V.".....V

This topic is closed to new replies.

Advertisement