Forward declarations in c++/cli
I am using VS 2008.
I'm having problems with specifying a "forward declaration". The class with it in compiles okay, but when the compiler meets a "gcnew xClass();" to instantiate an instance, the compiler "crashes" with a fatal error C1001h in compiler file 'mscl.cpp' line 1441.
my class starts -
#pragma once
using namespace System;
generic<class K, class V>
where K : IComparable
ref class xClass
{
ref struct bNode; //*** Needed to overcome the forward reference ***
ref struct kNode
{
K key;
V value;
kNode^ rLink;
bNode^ dLink;
kNode(K Key, V Value)
{
key = Key;
value = Value;
}
};
ref struct bNode
{
bNode^ parent;
int count;
kNode^ rLink; //*** THIS LINE CAUSES THE CRASH ***
bNode^ dLink;
};
...etc.
};
When I swap the struct blocks round its always the 2nd block's reference to the first block that causes the problem.
If I change the 'kNode^ rLink;' to 'Object^ rLink;' it compiles, but takes an age to do it! devenv.exe seems to go into a loop and lock up the system 100%. I've got to force the MSVC to via with ctrl-alt-del.
It seems that the compiler gets into an endless loop with this circular cross-reference. If I break it by removing either rLink in bNode, or dLink in kNode (and the forward ref line) the compier works okay.
Steve.
Re: Forward declarations in c++/cli
is there a semicolon missing after "ref struct kNode" ?
EDIT: oh its a nested structure... you'd better add code tags :sick:
Re: Forward declarations in c++/cli
I've found that if I do away with the generics and just hard code the types as String^ or int, the forward declaration works okay. Generics seems to be a bit of a bodge anyway with its weird syntax.
Pity that MS don't think its worthwhile to check these things out.
Steve.
Re: Forward declarations in c++/cli
I've tried various ways to work around this problem. First I tried making my C++/CLI class into a dll, then calling it from C++ code. The class compiled, but the calling program failed to compile; the compiler crashed and offered to "phone home" to Microsoft with the error information. (like it did previously).
Next I tried to use this dll from C# code. This compiled okay but crashed at runtime. I get a runtime error of System.TypeLoadException. It seemed to instantiate okay but failed when it tried to access the methods in xClass.
So now I converted my class source into C#. C# will accept forward references without needing forward declarations. I compiled this into a dll.
This dll will run okay from a C# main program, but still fails to compile from a C++ main program.
It seems that the C++ compiler does not like mixing generics with forward references. This also shows that you can't write a dll in C#, have it run okay from C#, then expect to be able to use it from C++ as well.
I am still using MSVS from 2008, but like with the earlier 2005 version I'm getting frequent blue screen crashes. I am a bit reluctant to move up to MSVC 2010 - why are Microsoft having to make so many updates before they get it right?