c++ - 使用 MFC 时由于 "__cdecl"和 "__thiscall"调用约定不匹配导致的链接器错误?

标签 c++ visual-studio-2008 calling-convention

我正在使用 Visual Studio 2008。仅当使用 MFC CString(与 std::wstring 相比)构建包含静态链接库的项目时,我才收到链接器错误。

所以这是可行的:

//header
class FileProcessor
{
    public:
        class iterator;
        friend class iterator;
    //...

    class iterator : public std::iterator<std::input_iterator_tag, std::vector<std::vector<std::wstring>>>
    {
    public:
    //...
    std::vector<std::vector<std::wstring>> operator*() const; 
    }
} 


//cpp 
std::vector<std::vector<std::wstring>> FileProcessor::iterator::operator*() const
{
    return _outerRef->_pimpl->_saxHandler->GetCurrentTable();
}       

但是这个

//header
#include <afx.h>

class FileProcessor
{
    public:
    class iterator;
    friend class iterator;
    //...

    class iterator : public std::iterator<std::input_iterator_tag, std::vector<std::vector<CString>>>
    {
    public:
        //...
        std::vector<std::vector<CString>> operator*() const; 
    }
} 


//cpp 
std::vector<std::vector<CString>> FileProcessor::iterator::operator*() const
{
    return _outerRef->_pimpl->_saxHandler->GetCurrentTable();
}

给出链接器错误:

FileProcessingTests.obj : error LNK2019: unresolved external symbol "public: class
std::vector<class std::vector<class std::basic_string<wchar_t,struct 
std::char_traits<wchar_t>,class std::allocator<wchar_t> >,class std::allocator<class 
std::basic_string<wchar_t,struct std::char_traits<wchar_t>,class 
std::allocator<wchar_t> > > >,class std::allocator<class std::vector<class 
std::basic_string<wchar_t,struct std::char_traits<wchar_t>,class 
std::allocator<wchar_t> >,class std::allocator<class std::basic_string<wchar_t,struct 
std::char_traits<wchar_t>,class std::allocator<wchar_t> > > > > > __thiscall 
FileProcessing::FileProcessor::iterator::operator*(void)const "
(??Diterator@FileProcessor@FileProcessing@@QBE?AV?$vector@V?$vector@V?$basic_string@_WU?
$char_traits@_W@std@@V?$allocator@_W@2@@std@@V?$allocator@V?$basic_string@_WU?$char_traits
@_W@std@@V?$allocator@_W@2@@std@@@2@@std@@V?$allocator@V?$vector@V?$basic_string@_WU?$
char_traits@_W@std@@V?$allocator@_W@2@@std@@V?$allocator@V?$basic_string@_WU?$char_traits
@_W@std@@V?$allocator@_W@2@@std@@@2@@std@@@2@@std@@XZ) referenced in function "void 
__cdecl TestUnicodeSingleTable(void)" (?TestUnicodeSingleTable@@YAXXZ)

在这两个项目中,调用约定在项目文件中指定为 __cdecl。那么为什么 __thiscall 会出现,我该如何解决呢?我必须使用 __cdecl,因为我正在引用使用 __cdecl 的外部库。

其他项目设置:

两个项目都有这些配置设置:

  • “MFC 的使用”:在共享 DLL 中使用 MFC
  • “使用 ATL”:不使用 ATL
  • “字符集”:使用Unicode字符集

最佳答案

看起来 FileProcessingTests.cpp 没有被重建,或者它使用的是陈旧的 header 。该目标文件仍在尝试链接到 std::wstring 变体而不是 CString 变体。

错误消息是一种冗长的方式,说明未解析的外部符号是用于:

std::vector<std::vector<std::wstring>> FileProcessor::iterator::operator*() const

请记住 std::wstring 只是一个 typedef 用于:

class std::basic_string<wchar_t,struct std::char_traits<wchar_t>,class std::allocator<wchar_t> >

如果您使默认模板参数显式。错误消息还使 std::vector 的默认模板参数也显式化,这就是错误消息如此可怕的原因。

关于c++ - 使用 MFC 时由于 "__cdecl"和 "__thiscall"调用约定不匹配导致的链接器错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12344524/

相关文章:

c++ - Linux 如何知道进程使用了​​多少物理内存?

c++ - 派生自同一类但不共享公共(public)方法的两个对象的类设计

c++ - 为什么这个对象没有检测到父类?

c++ - G++编译器无法找到iostream所需的 header

c++ - 将 Gravity(一种脚本语言)调用转换为 native C 函数调用

c++ - 什么是自定义调用约定?

c - 为什么我的exe文件在其他电脑上无法运行? [调试]

visual-studio-2008 - 如何使用 Visual Source Safe 2005?

c# - 如何在负载测试中避免 SSL 握手?

delphi - stdcall和winapi指令之间的区别?