views:

114

answers:

2

I just right now "migrated" from C# to C++/CLR. First I was annoyed, that I had to write all class' declarations twice (into .h and .cpp). Then I figured out, that I could place the code also into the h-files - it compiles at least. Well, I deleted all cpp's of my classes and now I realized, VS won't give me any Intellisense when I work on my h-files.

I guess I should not place my code in the hfiles (the code won't be reused in other projects for sure), but I find it terrible to adjust all method declarations at two places... Plus I have to switch back and forth to see what modifier my method etc. and it is not nicely all in one place like in C# (with it's pros and cons).

I'm sorry this is a newbie question, but I just wanted to make sure that there isn't any possibility to enable intellisense for hfiles. Or at least to learn, that I am completely on the wrong path...

Thanks, David

+1  A: 

Your .h files should contain declarations. your .cpp files, definitions.

Here's an example:

b.h

#ifndef B_H    
#define B_H

    class B
    {
      public:
         int foo();
         void Set(int x);

      private
         int data_;
    };

#endif

b.cpp

#include <stream>
#include "b.h"

int B::foo()
{
   std::cout << "data value " << data_;
   return data_;
}

void B::Set(int x)
{
   data_ = x;
}

Any place you'll use objects of type B, you #include b.h. The implementation is only in b.cpp If you do this, intellisense should work

Liz Albin
Yup +1. That's what needs to happen.
Billy ONeal
`b.h` contains 2 `#endif`. The first one, after the `#define` should be removed. :-)
Thomas Matthews
@Thomas: corrected.
quamrana
A: 

You're blowing intellisense out of the water because code for every class is being inlined into every implementation file, and that's just more data than Intellisense can parse reliably. It starts failing due to timeouts.

I know you said it's annoying, but you have to put the class in twice -- that's how C++ works. If you want it to behave like C#, use C#. It's a better language for the .NET platform anyway.

Billy ONeal
Isn't there a tool/an addin that can accomplish placing the definitions automatically into h- and cpp-files?
David
@David: We wish!!
quamrana
@David: Even when VS2010 comes out (which fixes this particular intellisense problem) it's still not going to be good, because your resulting executable will be HUGE because code needs to be reimplemented in every .cpp file that uses a class if it's inlined like you describe in the header. Don't fight the language here -- that is how it is designed. If you don't like that, go back to C#. C++/CLI is more for being able to reuse existing C++ code, not for new development, to my (albeit limited) understanding.
Billy ONeal
@BillyONeal: Thanks for your detailed explanations!
David