Linker error due to "__cdecl" and "__thiscall" mismatch when using MFC?

I am using Visual Studio 2008. I am getting a linker error when creating a project that contains a statically linked library only when using MFC CString (vs std :: wstring).

So this works:

//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(); } 

But this

 //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(); } 

gives the linker error:

 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) 

In both projects, the calling convention is indicated as __cdecl in the project file. So why __thiscall arises, and how can I fix it? I have to use __cdecl because I reference external libraries that use __cdecl .

Additional project settings:

Both projects have the following settings:

  • "Using MFC": use MFC in a shared DLL
  • "Using ATL": not using ATL
  • Character Set: Use the Unicode character set.
+4
source share
2 answers

It seems that FileProcessingTests.cpp not being rebuilt or is using an outdated header. This object file is still trying to reference the std::wstring variant, rather than the CString variant.

An error message is a detailed way of saying that an unresolved external character is for:

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

Keep in mind that std::wstring is just a typedef for:

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

if you make the default template parameters explicit. The error message also makes the default template settings for std::vector explicit, so the error message has such a terrifying process.

0
source

From MSDN :

The __thiscall calling convention is used for member functions and the default calling convention used by C ++ member functions that execute does not use variable arguments.

Thus, __thiscall is a standard method call. This can be a problem.

I suggest trying to add __cdecl for the methods you have problems with, to see what happens.

0
source

Source: https://habr.com/ru/post/1433278/


All Articles