Page 1 of 1
Managed, Unmanaged code C++
Posted: Sat Aug 30, 2008 1:28 pm
by grimdoomer
Im playing around with C++, and I've use unmanaged code to learn some basics. But I wanted to make a app to loads tags in a halo map. So I created a CLR Forms project. And the class below keeps giving me errors, can not declare managed System::IO::BinaryReader in unmanaged class Map.
Code: Select all
#include "stdafx.h"
#pragma managed
namespace Test
{
using namespace System;
using namespace System::IO;
using namespace Test;
class Map
{
public:
FileStream^ fs;
BinaryReader^ br;
BinaryWriter^ bw;
Map(char* Location);
};
}
Can someone please help?
Re: Managed, Unmanaged code C++
Posted: Sat Aug 30, 2008 1:45 pm
by LuxuriousMeat
Your trying to use managed code in unmanaged code, thats a no no.
Posted: Sat Aug 30, 2008 3:12 pm
by grimdoomer
But all I did was create a class, whats to say its managed or not?
Posted: Sat Aug 30, 2008 3:25 pm
by xzodia
Well from the sounds of things you made an unmanaged project..therefore any class made in that project will be unmanaged...
Posted: Sat Aug 30, 2008 5:45 pm
by grimdoomer
But .NET is managed, isn't it?
Posted: Sat Aug 30, 2008 8:30 pm
by RapiD
In visual c++ you have a choice to create a managed class or an unmanaged class. The differences between managed code and unmanaged code are that unmanaged code is compiled directly to native machine code (otherwise known as native code), while managed code is compiled to a secondary language which is then compiled and ran on run-time.
In C# or Visual Basic, you don't have a choice; Your code is always compiled into the standard IL language. In Visual c++ you have a choice. In fact you can even do this per-class simply by defining your classes with "__gc ". An example would be my class known as "Player_Class":
Unmanaged
Code: Select all
class player_class
{
private:
int x, y, z;
}
Managed
Code: Select all
__gc class player_class
{
private:
int x, y, z;
}
which in actual terms mean "Garbage Collected". That is how you define managed code from unmanaged code. Please keep in mind that your project must then compile under CLR instead of into native machine code.
You can
technically used managed code together with unmanaged code, however that gets complicated. I will not get into details about that (although i'm sure a simple google search will provide you with more than enough information concerning that)
Posted: Sat Aug 30, 2008 8:35 pm
by grimdoomer
When I add __gc befor the class, I get an error saying I need to add /clr:oldsyntax to the command line options. When I do I get like 20 errors all over the place.
Posted: Sat Aug 30, 2008 8:42 pm
by RapiD
well send me a few of those errors and i can try to help there. This is why if you're going to have a major change in your project, make it at a very early stage because converting the project itself is a pain in the ass.
Posted: Sun Aug 31, 2008 5:15 am
by grimdoomer
Well it is a CLR rpoject, and is being compiled under /clr. So basically I can'y any new classes to a clr project without rewriting half the form code?
Posted: Mon Sep 01, 2008 9:26 am
by kornman00
grimdoomer wrote:Well it is a CLR rpoject, and is being compiled under /clr. So basically I can'y any new classes to a clr project without rewriting half the form code?
The code example he gave you was of MC++ which was in VS03, 05 and onward us C++\CLR (however you can tell it to use the old syntax like it was trying to hint to you) which is a different syntax (a far better syntax that the crap they gave us in MC++). Like they said, you declared a regular unmanaged C++ class and tried to use managed references, which is why it's giving you crap. I'd suggest you actually learn the C++ language then transition into learning C++\CLR before trying to actually code anything as you don't seem to understand what you're doing at all, and it won't help trying to just blindly slice and dice things together.
Posted: Mon Sep 01, 2008 9:35 am
by grimdoomer
Well, I can't learn anything unless I try it myself. And I would rather learn under .NET something I'm familiar with, then using random classes not having any idea what todo.