Unresolved Token in Managed C ++

I have a secret in my arms. I am trying to learn managed C ++ coming from C # background and am confused. If I have a project that includes two classes, the base class Soup and the derived class TomatoSoup , which I compile as a static library (.lib), I get unresolved markers for virtual methods in Soup . Here is the code:


Abstracts.proj

Soup.h

namespace Abstracts { public ref class Soup abstract { public: virtual void heat(int Degrees); }; } 

TomatoSoup.h

 #include "Soup.h" namespace Abstracts { public ref class TomatoSoup : Abstracts::Soup { public: virtual void heat(int Degrees) override; }; } 

TomatoSoup.cpp

 #include "TomatoSoup.h" void Abstracts::TomatoSoup::heat(int Degrees) { System::Console::WriteLine("Soup on."); } 

Main.proj

main.cpp

 #include "TomatoSoup.h" using namespace System; int main(array<System::String ^> ^args) { Abstracts::TomatoSoup^ ts = gcnew Abstracts::TomatoSoup(); return 0; } 

I get this link time error on Main.proj :

 1>Main.obj : error LNK2020: unresolved token (06000001) Abstracts.Soup::heat 
  • I tried setting

     virtual void heat(int Degrees)=0; 
  • I tried to realize heat in the base class

     virtual void heat(int Degrees){} 

    and get an unpublished formal warning parameter error.

  • I tried both 1 and 2 with no abstract keyword on the soup class

This problem is driving me crazy, and I hope to prevent it from driving other developers in the future.

UPDATE:. This worked with the Greg Hewgill name argument compilation method when TomatSoup :: heat was implemented in the header file, but the error returned when I moved the implementation to TomatoSoup.cpp. I changed the question to reflect this.

+3
source share
3 answers

The error you get (LNK2020) means that the linker cannot find the definition of the Abstracts.Soup::heat function anywhere. When you declare a function as virtual void heat(int Degrees); , the linker will expect the function body to be defined somewhere.

If you intend not to provide a function body and require subclasses to ultimately redefine this function, you should do as described in solution 1:

 virtual void heat(int Degrees) = 0; 

This tells the compiler that you are not going to perform this function. What happens when you do this?

Also, with reference to your second solution:

 virtual void heat(int Degrees) {} 

the compiler tells you that you are not referencing the Degrees parameter inside the function. One way to avoid this warning in C ++ is to not give the parameter a name:

 virtual void heat(int /*Degrees*/) {} 

Delivering an empty implementation is slightly different from a pure virtual declaration ( = 0 ).

+3
source

I think this error could happen because I tried to create Abstracts.proj as a static library. When I changed it to .DLL, everything worked fine.

I saw a mention that managed code is not supported from static libraries, but nothing official. Does anyone have a good link that claims this?

+2
source

I have little CLI experience, but I think you should inherit using public:

public ref class TomatoSoup: public Abstracts :: Soup

0
source

All Articles