Click to See Complete Forum and Search --> : headers?


_uj
June 25th, 2007, 10:30 AM
Normally in classic C++ you have a class on a definition file (.h) and an implementation file (.cpp).

I've added a ref class and it's both defined and implemented in a single .cpp file. To use this ref class I seem to have to include it like I would a .h. Is this how it should be or should the ref class somehow be split into .h and .cpp part or is there something else I should do?

Nowhere can I find an example of that ref classes must be included to be used so I guess I've missed something.

Krishnaa
June 25th, 2007, 11:34 AM
You need a header file which you can include it in other CPP files to use that class there.

VC++ IDE creates header file if yu create the class using wizard.

_uj
June 25th, 2007, 01:17 PM
You need a header file which you can include it in other CPP files to use that class there.


What should be on that header file? I've never seen an example of a ref class in C++/CLI being divided up into an .h and a .cpp file.

To me it seems only classic .h files are being included and that ref classes are being included by the use of "using namespace" or "#using".

So if I define a ref class on a .cpp file should I include this file in all files where the ref class is used? This would make the header and the implementation file the same file. Is this the preferred way? Well, it works but I cannot find any example of it in my books.

Krishnaa
June 26th, 2007, 08:08 AM
The header and CPP are same as previous, for example try adding a new class using Project->Add class menu.

_uj
June 26th, 2007, 01:25 PM
The header and CPP are same as previous


Yes I've come to realize that you have to split C++/CLI code into .h and .cpp files just like you do classic C++ code. What I find strange is that this fact is not mentioned in,

- Expert C++/CLI by Heege,
- C++/CLI by Hogenson,
- C++/CLI In Action by Sivakumar, or the
- ECMA-372 C++/CLI standard,

Nowhere in these references is a CLI type, say a ref class, ever split into a definition and an implementation part. In none of these references is it ever mentioned that this is what you have to do to work with C++/CLI in practice. That's what I find strange.

Am I missing something? Is it something with the Visual C++ compiler? Is it at all possible to keep CLI types on single source files like they seem to be able to in the references above?

darwen
June 26th, 2007, 05:18 PM
You've definately done something wrong. Try this (in DevStudio2005) :

In solution explorer, right click on your project.

Pick 'add/class'.

In the 'categories' tree on the left select C++.

Select 'C++ class' from the 'templates' on the right.

Click 'add'.

Enter your class name. You'll see that the .h & .cpp file names get set for you.

Ensure the 'managed' check box is ticked, click 'finish'.

Your class in the .h and .cpp has now been created for you.

Darwen.

_uj
June 27th, 2007, 02:10 AM
Your class in the .h and .cpp has now been created for you.

Thanks for the reply but what on earth makes you think I'm having problems with creating .h and .cpp files? :confused: