使用MSVC 2015构建链接器时出错(其他CC都可以)

时间:2015-10-29 18:07:58

标签: c++ visual-c++ gcc5.2

以下是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构建:好的

问题:

为什么?

如何修复/解决?

P.S。

在QtScript项目中

Original file

1 个答案:

答案 0 :(得分:1)

考虑到Breaking Changes in Visual C++ 2015我倾向于以下决定:

class Obj {
...
private:
    friend class MyClass;
    void* operator new(size_t, void*);
#if defined(_MSC_VER) && _MSC_VER >= 1900
    enum class Obj_tag : size_t {};
    void operator delete(void*, Obj_tag);
#else
    void operator delete(void*, size_t);
#endif
};

所以,以下代码

#ifdef ERROR_TEST
char buf[1024] = { 0 };
Obj * o1 = reinterpret_cast<Obj*>(buf);
new (o1) Obj;
#endif
MyClass o2;

以与MSVC 2012和MSVC 2015相同的方式编译。(定义{{​​1}}时有ERROR_TEST,何时不是 - 一切正常