CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Hybrid View

  1. #1
    Join Date
    Dec 2010
    Posts
    3

    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.

  2. #2
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    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
    Last edited by memeloo; December 10th, 2010 at 11:11 AM.
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  3. #3
    Join Date
    Dec 2010
    Posts
    3

    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.

  4. #4
    Join Date
    Dec 2010
    Posts
    3

    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?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured