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

3
  • 4
    Do you have a definition of constructor and destructor somewhere? If not, try appending = default to both declarations. Commented Jun 21, 2018 at 8:35
  • The whole any_cast business is a red herring, really. Commented Jun 21, 2018 at 9:37
  • @StoryTeller what do you mean? Commented Jun 21, 2018 at 9:42

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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 Destructor can be approached in the same 3 ways. There is nothing different about the definition here for a destructor.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.