Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

forward class references in managed C++

2 views
Skip to first unread message

G Gray

unread,
Sep 9, 2003, 12:13:13 PM9/9/03
to
I am having a problem using forward class references in managed C++ in
the same way I use them in unmanged code.

I have used Microsoft Visual C++ .NET to write a managed C++
application. The
application contains both managed and unmanaged code, contained within
difference files. The unmanged file is MyClass.cpp and contains the
definition
and implementation of class MyClass as well as a global object.

class MyClass {
public:
MyClass() {};
};
MyClass MyClassObject;

The managed file is ManagedC++.cpp and contains code that 'referneces'
global
object MyClassObject. Note that a forward class reference is used in
this file
instead of the class definition.

#using <mscorlib.dll>

class MyClass;
//class MyClass { public: MyClass(); int value; };
extern MyClass MyClassObject;

void managedFunction() {
MyClass *local = & MyClassObject;
};

When I execute this code within a managed C++ application, the
"Just-In-Time Debugging"
dialog box is displayed, followed by the following message:

Unhandled Exception: System.TypeLoadException: Could not load type
MyClass from assembly SimpleManagedC++App, Version=1
0.1347.19247, Culture=neutral, PublicKeyToken=null.

If I uncomment out the line
//class MyClass { public: MyClass(); int value; };
in the ManagedC++.cpp file, then the application executes without
exception.

Thanks in advance for any help that can be provided.

Tomas Restrepo (MVP)

unread,
Sep 9, 2003, 7:42:32 PM9/9/03
to
G,

Is your MyClass class implemented in a different DLL or something? If it is
within the same binary, you shouldn't have a problem. Otherwise, providing
an empty definition in the managed side should be enough to avoid the error
(this is basically the same problem presented in [1])

[1] http://www.winterdom.com/mcppfaq/archives/000262.html

--
Tomas Restrepo
tom...@mvps.org


G Gray

unread,
Sep 10, 2003, 9:19:02 AM9/10/03
to
"Tomas Restrepo \(MVP\)" <tom...@mvps.org> wrote in message news:<eNkGNwyd...@tk2msftngp13.phx.gbl>...


Thomas,

Thank your for your helpful response. In the case I described above
all source code is included within a single C++ .NET 2002 application.
The class MyClass is not implemented in a different DLL. The class is
implemented in a separate source file that is not compiled with the
/clr compiler option, so will be compiled as native code.

I followed the link you provided. It is helpful to know that others
have run into this situation. I put the above test application
together to demonstrate a problem I am having with a very large code
base that has been compiled into a mixed mode DLL using .NET C++ 2002.
In this code base, there exists hundreds of classes, who's definitions
are contained in .cpp files, and are not exposed through a header
file. The aforementioned .cpp files are compiled as unmanaged code.
Header files, containing forward class references of these classes are
included within .cpp files that are compiled as managed code. The
mixed mode DLL builds successfully. But when I try to load the DLL
from a C# application I get a similiar type of exception as listed
above, e.g. "Unhandled Exception: System.TypeLoadException...". But
when executing the C# application, the specific type that could not be
loaded is not listed in the exception message. So now I have to
search through a very large code base to determine each of the classes
that are defined in .cpp files and forward declared in headers, so
that I can apply the fix that you have suggested, i.e. using an empty
class declaration inplace of a forward class definition.

Thanks again for your help.

Greg Gray

Tanveer Gani [MSFT]

unread,
Sep 12, 2003, 1:36:28 PM9/12/03
to

--------------------
>From: greg...@kodak.com (G Gray)
>Newsgroups: microsoft.public.dotnet.languages.vc
>Subject: Re: forward class references in managed C++
>Date: 10 Sep 2003 06:19:02 -0700
>Organization: http://groups.google.com/
>Lines: 88
>Message-ID: <ceabbce1.03091...@posting.google.com>
>References: <ceabbce1.03090...@posting.google.com>
<eNkGNwyd...@tk2msftngp13.phx.gbl>
>NNTP-Posting-Host: 165.170.128.66
>Content-Type: text/plain; charset=ISO-8859-1
>Content-Transfer-Encoding: 8bit
>X-Trace: posting.google.com 1063199944 32228 127.0.0.1 (10 Sep 2003
13:19:04 GMT)
>X-Complaints-To: groups...@google.com
>NNTP-Posting-Date: 10 Sep 2003 13:19:04 GMT
>Path:
cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.sul.t-online.de!t-onlin
e.de!newsfeed.icl.net!newsfeed.fjserv.net!newshosting.com!news-xfer2.atl.new
shosting.com!sn-xit-03!sn-xit-01!sn-xit-09!supernews.com!postnews1.google.co
m!not-for-mail
>Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vc:28163
>X-Tomcat-NG: microsoft.public.dotnet.languages.vc

Greg,

C++ being different than other languages in that it supports separate
compilation has unique problems with regard to meta-data for classes in
headers. It so happens sometimes that the linker is too aggressive in
eliminating meta-data that it thinks is not referenced. I think you're
running across this problem.

For each type that's not loaded, you can do this:

1. For the .OBJ where the class is defined, dump it's metadata. You can
use ildasm.exe to do this as:

ildasm /text /out=meta.txt objname.obj

Look for the "TypeDef" for the particular class in meta.txt


2. Use ildasm on your EXE/DLL and see if the class doesn't show up.

You may find that the class meta-data is present in the .OBJ but eliminated
by the linker from the EXE/DLL. To prevent this, make sure the class is
referenced in the .OBJ where it's defined.

If you provide more details, someone on the VC++ team can help you better.


--
Tanveer Gani, Microsoft Visual C++ Team
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm


0 new messages