VS 2017 C++编译器在 VS 2005 中找不到匹配函数

VS 2017 C++ compiler cannot find matching function was OK in VS 2005

本文关键字:VS 找不到 函数 2005 2017 C++ 编译器      更新时间:2023-10-16

我正在得到

">

错误 C3861:"to8Bit":找不到标识符"在此情况下:

typedef struct _ustruct2
{
int which;
union 
{
double d;
tstring  * s;
bool b;
char e;
} uu;
} ustruct2;
std::string  to8Bit(const std::string &s);
std::string  to8Bit(const std::string s);
std::string  to8Bit(const tstring s);
std::string  to8Bit(tstring s);
std::string  to8Bit(tstring &s);
std::string  to8Bit(wchar_t * pc);
std::string  to8Bit(const tstring &s);
std::string  to8Bit(const char * pc, int len = -1);
std::string  to8Bit(const wchar_t * pc, int len = -1);
static void outputustruct(FILE * opmte, int i, const char * s, const ustruct2 &u)
{
std::string mys2 = to8Bit((*u.uu.s).c_str());
}

还有一些其他版本的to8BIT我认为这篇文章不需要。 但我认为其中一个会匹配。 我尝试去掉函数输出结构函数标题中的常量和/或与号,并在删除与号时删除to8Bit调用中的星号。 我已经尝试了结构定义中的字符串和 tstring 以及带有错误的代码,但我所做的一切都无法编译。

此代码在VS 2005中工作正常,但在VS 2017中无效。 根据我的经验,MS 在更高版本中收紧编译器以更严格地符合标准,但我看不出是什么导致了这里的编译器错误。

C++11std::wstring_convert(在 C++17 中已弃用(提供了一个简单的解决方案:

在您的情况下,字符串定义为:

#include <tchar.h> // For _TCHAR
typedef std::basic_string<_TCHAR> tstring;

可以像这样转换:

#include <locale>
#include <codecvt>
int main()
{
tstring string_to_convert = L"Follow the white rabbit";
// Converter setup:
using convert_type = std::codecvt_utf8<wchar_t>;
std::wstring_convert<convert_type, wchar_t> converter;
// Convert (.to_bytes: wstr->str, .from_bytes: str->wstr)
std::string converted_str = converter.to_bytes(string_to_convert);
return 0;
}

或者在您的情况下,如下所示:

#include <locale>
#include <codecvt>
static void outputustruct(FILE * opmte, int i, const char * s, const ustruct2 &u)
{
// Converter setup:
using convert_type = std::codecvt_utf8<wchar_t>;
std::wstring_convert<convert_type, wchar_t> converter;
// Convert:
string mys2 = converter.to_bytes(*u.uu.s);
}

(这适用于VS2017 C++(