Problem with VC++ 2003.Net Inheritance and destructors

Sad I have come upon a weird but probably very simple problem. In Visual Studio.net 2003 I try inheriting a public managed class into a public managed class both of which were created by using the class wizard. If I comment out the destructors the library compiles properly. But if I leave in the destructors or any one of them the inheritance gives a compiler error.
Would someone be kind enough to tell me what silly mistake I am making
Error Message is fatal error LNK1120: 1 unresolved externals
#pragma once
using namespace System;
namespace StationObjects
{
public __gc class StationObj
{
public:
   StatioObj(void){}
   ~StationObj(void){}
};
__gc class PressObj : public StationObj
{
public:
PressObj(void){}
~PressObj(void){}
};
}


Answer this question

Problem with VC++ 2003.Net Inheritance and destructors

  • Yew Thean Hoo

    You can just disable precompiled headers.

    Ok, I tried the following and it worked as expected:
    1) cl /CLR /c i.cpp
    2) lib i.obj

    where i.cpp and i.h look like the following:
    //i.cpp
    #using <mscorlib.dll>
    using namespace System;
    //#include "stdafx.h"
    #include "i.h"
    using namespace StationObjects;

    //i.h
    #pragma once
    namespace StationObjects
    {
    public __gc class StationObj
    {
    public:
       StationObj(void){}
       ~StationObj(void){}
    };
    __gc class PressObj : public StationObj
    {
    public:
    PressObj(void){}
    ~PressObj(void){}
    };
    }

    The only difference is the stdafx.h (what does yours look like). Also, could you please try the exact above sample from the VC command window and see if you get any errors.

    Thanks,
      Ayman Shoukry
      VC++ Team



  • IbrahimSA

    Here is the problem I am having:

    // This is the main DLL file.
    #include "stdafx.h"
    using namespace System;
    public __gc class StationObjects
    {
    public:
       StationObjects()
       {
       Int32 x = 1;
       }

       // ~StationObjects()
       //{
       //}
       // TODO: Add your methods for this class here.
    };

    public __gc class PressObj : public StationObjects
    {
    public:
       PressObj()
       {
       }
       ~PressObj()
       {
       }
    };

    This will compile but gives an error when you build it. If I comment out the destructor I can build it with out the error. To me this is very weird. It must have something to do with the way managed garbage collection works because unmanaged code works fine. For now I am going to build my class objects without destructors.
    I really hope you can answer this one. Tongue Tied



  • amidar

    Hi again Ayman. I copied your code into a cpp file in a solution and got fatal error C1010: unexpected end of file while looking for precompiled header directive when I tryed to build it from the IDE.
    If this helps

  • YKen

    I just tried your sample below (compiled as cl /CLR /c a.cpp) and generated the lib by doing lib a.obj. All behaved as expected with no errros. Please include extra info on the error you are seeing plus the compilation commands.

    //a.cpp
    #pragma once
    #using <mscorlib.dll>
    using namespace System;
    namespace StationObjects
    {
    public __gc class StationObj
    {
    public:
       StationObj(void){}
       ~StationObj(void){}
    };
    __gc class PressObj : public StationObj
    {
    public:
    PressObj(void){}
    ~PressObj(void){}
    };
    }

    Thanks,
      Ayman Shoukry
      VC++ Team

  • IvanLieb

    Hi Ayman: I compiled your code using Build.Compile in the command window.
    I tried to compile my other file the same way and it tells me Build.Compile is unavailable. It also does not give the compile option in the top menu. Also I cannot seem to use the cl i.cpp/CLR command line for your code it tells me cl is not a recognized command.
    A secondary problem that I am having is that every time I start VISIO Standard it asks me if I want to configure Visual Studio and then it does it's thing asking for the VS disk to be inserted and does its thing. But it does this every time I start Visio.
    I don't know if both problems are related or not. Tongue Tied
    Help

  • SanderV

    Hi again Ayman: I have been trying to figure out this problem some more. I could compile my code but not build it so I figure the problem is with the linker.
    Is there a way I can send you the whole program so you can take a good look at what I am doing wrong. Tongue Tied This has stopped me in my tracks and I cannot seem to get around it. I tried building a seperate solution and as soon as I added the class PressObj which inherits from StationObj the build fails.
    Please Help!!!!! Tongue Tied

  • chrisan

    I just tried the below sample and it worked with no errors:
    The compilation command I used was cl /LD /CLR a.cpp and a.dll was generated.
    //a.cpp
    //#include "stdafx.h"
    #using <mscorlib.dll>
    using namespace System;
    public __gc class StationObjects
    {
    public:
       StationObjects()
       {
       Int32 x = 1;
       }
       ~StationObjects()
       {
       }
       // TODO: Add your methods for this class here.
    };
    public __gc class PressObj : public StationObjects
    {
    public:
       PressObj()
       {
       }
       ~PressObj()
       {
       }
    };

    Could you tell me what is in stdafx.h and also what is the compilation command line used (you can get this by viewing the build log file).

    Also, you can use the command prompt compilation (cl.exe) by starting visual studio command prompt from the windows Start menu:
    Start->All Programs->Microsoft visual Studio .Net 2003->Visual Studio .Net Tools->Visual Studio .Net 2003 command Prompt

    This will start a command prompt with the compiler tools (cl.exe) in the path already.

    Thanks,
      Ayman Shoukry
      VC++ Team



  • JaganG

    Thank you Ayman but it still is not working right.
    The above code was a .h file and the cpp file is below:
    #using <mscorlib.h>
    using namespace System;
    #include "stdafx.h"
    #include "TestInheritance.h"
    using namespace StationObjects;

    I am trying to create an object library.
    The error message is:
    error LNK2001: unresolved external symbol "void__cdedl_CxxCallUnwindDtor(void(__thiscall*)(void*)"( __CxxCallUnwindDtor@@$$JOYAXP6EXPAX@Z0@Z)
    error LNK1120: 1 unresolved externals

    I am using the Visual Studio IDE to build the solution by selecting build from the menu.
    Please help. I know I am doing something silly but I cannot seem to solve this.

  • Problem with VC++ 2003.Net Inheritance and destructors