Adding and using C++ class to Visual C++ project

Hi,

Have some really stupid questions about the Visual C++ 2005 environment that I hope someone can help me with... I have a Visual C++ 2003 book but there seams to be alot differences with the 2005 version.

I am new to Managed C++. I have previously used C# a lot and I wanted to learn more about .NET. I have created a Managed C++ project and I have created a Windows Form with some components. I have no problem writing code in this Windows Form class. My problem starts when I add a class to my project. When I add a new class in a C# project I am used to the class adding itself in the same namespace and with the class definition and everything. I just start coding and there are no problems. With Managed C++ it is different I have noticed.

I right click on my projects name in the solution explorer and choose Add and then Class. A window opens and I have written foo. I let the clas be public and the checkbox Managed checked. Two files are created foo.h and foo.cpp. They look like this:

foo.cpp:
#include "foo.h"
foo::foo(void)
{

}

foo.h:
#pragmaonce
ref class foo
{
public:
foo(void);
};

I can add function declarations in the foo.h class and add e.g. foo::newMethod to the foo.cpp class and the compilation will pass..

My problems are:

How would I instantiate my class from the Windows Form. I have tried the following example and some other examples as well without any result:

foo &f = *new foo();

My questions are:

1. How would I instantiate the foo class from the Windows Form code view? I have tried the following example and some other examples as well without any result:foo &f = *new foo();

2. Why arent the class definnition and namespace visible like in a C# project.

3. Why use the foo::foo notation?

4. What does #pragma once mean?

I really hope that I could get some answers. I have not been able to find the information elsewhere...

/M

[4386 byte] By [Mortsdeh] at [2007-12-24]
# 1

1. In VC++ 2005 Managed C++ (now called C++/CLI) changed a lot so your old book about 2003 won't be of much use. In this case the equivalent code is:

foo^ f = gcnew foo();

There is a new declarator, ^, that declares a handle (reference) to a .NET object and you also have a new allocation operator, gcnew, that is used for allocating .NET objects.

See here for the documentation:

http://msdn2.microsoft.com/en-us/library/yk97tc08.aspx

2. I don't quite understand this question, what do you mean by "visible" ?

3. This is what C++ uses to specify that a function is a part of a class otherwise there is no way for the compiler to know what to do with the function. :: is the scope resolution operator: http://msdn2.microsoft.com/en-us/library/b451xz31.aspx

4. #pragma once is used to tell the compiler to only include that file once. Due to the way header files work in C/C++ it's possible that the same header file to be included more than once when compiling a cpp file. Without #pragma once the result will be that the compiler finds multiple declaration of the class from the header file and gives an error.

Truth to be told altough C# and C++ have many similartities there are also many differences and reading a good book about plaing old C++ (not managed) would be a start.

MikeDanes at 2007-8-31 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 2

Thank you for your answer. It was very helpful.

Still, I have problems compiling my code. On the links you provided I read that the compiler option /clr should be on. Could that be it?

On this line:

foo ^ f = gcnew foo();

I get these error messages:

error C2065: 'foo' : undeclared identifier
error C2065: 'f' : undeclared identifier
error C2061: syntax error : identifier 'foo'

foo.cpp:
#include "StdAfx.h"
#include "foo.h"
foo::foo(void)
{
}

foo.h:
#pragma once
ref
class foo
{
public:
foo(
void);
};

Grateful for answers...
Thanx /M

Mortsdeh at 2007-8-31 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 3

/clr is already on if you create a WinForm project.

Most probable cause for getting those error is that you did not include "foo.h" in the file where you do gcnew. You need to add

#include "foo.h"

at the top of the file that contains gcnew foo();

Note: If the file also contains the line #include "stdafx.h" be sure to add #include "foo.h" AFTER that line.

MikeDanes at 2007-8-31 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 4

Thank you. You have been most helpful.

/M

Mortsdeh at 2007-8-31 > top of Msdn Tech,Windows Forms,Windows Forms General...