ref friend class? RRS feed

  • Question

  • I'm using the Unit Testing framework of VS2005 and I'm running into a problem I'm trying to access private member functions of a NONmanaged class with a managed test class like so:

    class CBob
        static int PrivateFunctionToTest();

        friend class TestCBob;

    ref class TestCBob
        void TestPrivateFunction()

            Assert::AreEqual(0, CBob::PrivateFunctionToTest());

    This doesn't work, however, because when it goes to compile the TestCBob, I get this error:

    Error 29 error C3816: 'class TestCBob' was previously declared or defined with a different managed modifier XYZ.h 60

    Is there any way to make a managed class a friend of a non-managed class?


    Thank you.

    Friday, July 7, 2006 11:09 PM


  • The C3816 error is telling you that the type TestCBob was declared differently in two different places. This is a common mistake when you first declare a class as a “ref class” and then later as a regular “class” or vice versa. And that’s what you’re doing wrong here. The friend specifier is declaring TestCBob as a native class and then you declare TestCBob as a ref class.

    The solution is to change the friend declaration as follows:

    friend ref class TestCBob;

    Kenny Kerr


    Saturday, July 8, 2006 2:55 AM