我在Visual Studio 2013更新4中有一段c ++代码(在Windows驱动程序中,但这对于我们的讨论可能并不那么重要)
我有这个分配器实现:
class allocator {
public:
pointer allocate(size_type n, typename allocator<void, TAG>::const_pointer = 0) {
return static_cast<pointer>(::ExAllocatePoolWithTag(NonPagedPool, n*sizeof(value_type), tag()));
}
void deallocate(pointer p, size_type) {
::ExFreePoolWithTag(p, tag());
}
void construct(pointer p, value_type const& val) {
::new(p)value_type(val);
}
void destroy(pointer p) {
p->~value_type();
}
}
我有一个基本类,它有一个定义的析构函数,导致我为场景编译错误:
struct A {
A(int i) : i(i) {
::DbgPrint("A::A");
}
~A() {
::DbgPrint("A::~A");
}
int i;
};
void Allocator_support_works_as_expected() {
allocator<A> alloc;
auto p = alloc.allocate(1);
alloc.construct(p, A(1));
alloc.destroy(p); // IF i comment this out it compiles, but no destructor called
alloc.deallocate(p, sizeof(A));
}
我在尝试编译时得到了以下信息:
错误消息:
error LNK2019: unresolved external symbol "void __cdecl operator delete(void *)" (??3@YAXPEAX@Z) referenced in function "public: void * __cdecl A::`scalar deleting destructor'(unsigned int)" (??_GA@@QEAAPEAXI@Z)
在&#34;销毁&#34;实现我调用类析构函数,因为allocator使用placement new。发生了什么事?
编辑1
似乎对于Windows驱动程序开发环境,我必须定义全局运算符new,即使在我的情况下我不使用它
答案 0 :(得分:1)
确保没有愚蠢的错误,例如
"c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86
"C:\Program Files (x86)\Microsoft Visual Studio 14.0\vc\bin\cl.exe" /EHsc /nologo /W4 test.cpp
我不小心为不同版本的MSVC设置了环境,我花了15分钟才意识到问题出在哪里