4

我正在使用 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 字符集
4

2 回答 2

0

来自MSDN

__thiscall 调用约定用于成员函数,是不使用变量参数的 C++ 成员函数使用的默认调用约定。

所以 __thiscall 是方法的默认调用约定。这可能是问题所在。

我建议尝试为您遇到问题的方法添加一个 __cdecl 以查看会发生什么。

于 2012-09-10T04:45:06.223 回答
0

它看起来FileProcessingTests.cpp没有被重建或者它正在使用一个陈旧的标题。该目标文件仍在尝试链接到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设为显式,这就是错误消息如此可怕的原因。

于 2012-09-10T06:01:04.727 回答