1

I'm new to some boost feature and I'm facing some issues trying to cast a reference to boost::any to a reference to a custom class (for now it's empty, I'm still figuring out the content of the class).

Shortly, I have:

class MyClass
{
public:
    MyClass();
    ~MyClass();
private:
}

MyClass function(boost::any &source)
{
    if (source.type() == typeid(MyClass))
        return boost::any_cast<MyClass>(source);
}

I've not implemented yet the constructors and destructor, so they're still the default ones.

While compiling (in Visual Studio 2017) I get the following message:

Severity Code Description Project File Line Suppression State Error LNK2001 unresolved external symbol "public: __thiscall MyClass::~MyClass(void)" (??1MyClass@@$$FQAE@XZ) NativeToManagedBridge C:\bridge_library\testCli_sources\NativeToManagedBridge\anyHelper.obj 1

trungduc
  • 11,926
  • 4
  • 31
  • 55
XectX
  • 36
  • 4

1 Answers1

1

You have declared your default constructor and destructor with MyClass(); and ~MyClass(); respectively. What does this mean? You are telling the constructor; "please don't implement a constructor or destructor for me, I will do it". If now, you don't define them, you will get the linker error you are seeing, because the compiler does not know where to find the definition of your destructor. You can solve this in multiple ways:

  1. Explicitly tell the constructor to use the default definition: MyClass() = default.
  2. Don't list the constructor declaration to allow the compiler to define it automatically.
  3. Define your constructor: MyClass() {}.

You can read more about definition and declaration here.

Fantastic Mr Fox
  • 32,495
  • 27
  • 95
  • 175
  • While with the constructor the problem is easy solved, with the destructor I cannot use the way at point 3. Is there a specific reason why that can't be done? I've always written constructor and destructor at the time I needed, and it's new to me this issue. – XectX Jun 21 '18 at 09:26
  • @XectX Destructor can be approached in the same 3 ways. There is nothing different about the definition here for a destructor. – Fantastic Mr Fox Jun 21 '18 at 09:33