这是MCVE:
#include <iostream>
#include <string>
using namespace std;
class Obj {
public:
Obj() { cout << "Obj()" << endl; }
~Obj() { cout << "~Obj()" << endl; }
void* operator new(size_t sz){
return ::operator new(sz);
}
void operator delete(void* p) {
::operator delete(p);
}
private:
friend class MyClass;
void* operator new(size_t, void*);
void operator delete(void*, size_t);
};
class MyClass {
public:
MyClass() : m_(new Obj) {
cout << "MyClass()" << endl;
}
~MyClass() {
cout << "~MyClass()" << endl;
delete m_;
}
private:
const Obj * m_;
};
int main()
{
cout << "Started" << endl;
MyClass o;
cout << "Finished" << endl;
return 0;
}
使用 MSVC 2015 (14.0) 构建:
error LNK2019: unresolved external symbol "private: static void __cdecl Obj::operator delete(void *,unsigned int)" (??3Obj@@CAXPAXI@Z) referenced in function __unwindfunclet$??0MyClass@@QAE@XZ$0
使用 MSVC 2013 (12.0) 构建:好的
使用 GCC 5.2 构建:好的
问题:
为什么?
如何修复/解决?
附言