XectX
XectX

Reputation: 36

boost::any_cast to my custom class

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

Upvotes: 1

Views: 338

Answers (1)

Fantastic Mr Fox
Fantastic Mr Fox

Reputation: 33864

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.

Upvotes: 1

Related Questions