Cannot make cyclic reference to value classes through arrays (C++/CLI)

Here is an example:

-----------------------------------------------
value class Class2;

value class Class1
{
public:
void method(array<Class2>^ arr); // Error (use of undefined type)
};

value class Class2
{
public:
void method(array<Class1>^ arr);
};
------------------------------------------------


Although Class2 is pre-declared, the compiler throws a "use of undefined type 'Class2'" error. Is there any way to compile this example


Answer this question

Cannot make cyclic reference to value classes through arrays (C++/CLI)

  • nairB

    The surest way to get a response from the compiler team (by design or fix--I don't really know) is to file a bug under http://lab.msdn.microsoft.com/productfeedback/.


  • Vi Truong

    But by restructuring your code so there's no mutual referencing, you may be still able to use array<class1>.
  • Acolin

    Brian Kramer wrote:
    But by restructuring your code so there's no mutual referencing, you may be still able to use array<class1>.


    That's true but the point that I'm trying to make is that this is a limitation (or a bug) of the C++/CLI compiler. It certainly is not a limitation of the .NET framework, the following example in C# will compile fine:

    -----------------------------------------------
    struct Class1
    {
    public void method(Class2[] arr)
    {
    }
    }

    struct Class2
    {
    public void method(Class1[] arr)
    {
    }
    }
    ------------------------------------------------


  • Mal409

    Probably not.  You'll might have to resort to using references (which defeats the purpose of value classes, I know).

    void method(array<Class2^>^ arr); 

    The same restriction occurs with native C++ programming.  You can only declare references or pointers to things that are forward declared.

     


  • Chanduu

    This is a serious problem of the C++/CLI compiler, it won't accept a value class inside an array unless it's fully defined. As the example I provided shows, this is not always possible.
  • pamskate5

    The corresponding case should work just fine in standard C++ because arrays are reference types.

    This actually looks like a compiler bug to me. Event if array<Class2> would require Class2 to be complete, array<Class2>^ shouldn't.

    All that being said, I don't see a better workaround either.

    -hg


  • Cannot make cyclic reference to value classes through arrays (C++/CLI)