VS 2017 C++ compiler cannot find matching function was OK in VS 2005 [closed]
Clash Royale CLAN TAG#URR8PPP
VS 2017 C++ compiler cannot find matching function was OK in VS 2005 [closed]
I am getting
"error C3861: 'to8Bit': identifier not found" in this situation:
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());
There are some other versions of to8BIT
that I don't think are needed in this post. But I would have thought one of these would match. I have tried getting rid of the const and/or the ampersand in the function outputustruct function header, and removing the asterisk in the call to to8Bit
when the ampersand is removed. I have tried string and tstring in the struct definition and the code with the error on it, but nothing I do gets it to compile.
to8BIT
to8Bit
This code works fine in VS 2005 but not in VS 2017. In my experience MS tightens the compiler in later versions to more strictly conform to standards but I can't see what's causing the compiler error here.
This question appears to be off-topic. The users who voted to close gave this specific reason:
I have a couple of to8Bit prototypes without the const. If I remove the const from the third argument to the outputustruct function I still get the same error.
– RichT
Aug 10 at 15:17
@SergeyA It isn't, but good luck calling either overload ;)
– Lightness Races in Orbit
Aug 10 at 15:58
OP you cannot be getting that error from this code. I would expect an ambiguous call but not that. Please present your Minimal, Complete, and Verifiable example.
– Lightness Races in Orbit
Aug 10 at 15:59
Why are you using
typedef struct _tagname
? That is C and completely unnecessary in C++. In C++, you simply define struct ustruct2 ...
– Adrian W
Aug 10 at 16:41
typedef struct _tagname
struct ustruct2 ...
1 Answer
1
C++11 std::wstring_convert
(deprecated in C++17) provides a simple solution:
std::wstring_convert
In your case, tstring defined as:
#include <tchar.h> // For _TCHAR
typedef std::basic_string<_TCHAR> tstring;
it is possible to convert like this:
#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;
or in your case, something like:
#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);
(This works on VS2017 C++)
I apologize in advance for my confusion when it comes to strings and characters. Is the change you made to ustruct2 where you have "std::tstring * ts;" instead of "tstring * s;" impacting anything? I don't want to break anything else in the larger project. That said I get the error "error C2039: 'tstring': is not a member of 'std'" in the struct definition and "error C2065: 'USES_CONVERSION': undeclared identifier" Is there something missing?
– RichT
Aug 10 at 15:52
I left the definition of ustruct2 as it was originally, in case that matters. I get "error C2039: 'codecvt_utf8': is not a member of 'std'" and "error C2039: 'wstring_convert': is not a member of 'std'" When you say wstring_convert was deprecated in 2017 do you mean VS2017? If so isn't that a problem since I'm upgrading to VS2017?
– RichT
Aug 10 at 18:12
@RichT #include <locale> and #include <codecvt>
– Amit G.
Aug 10 at 18:28
No luck: error C2664: 'std::basic_string<char,std::char_traits<char>,std::allocator<char>> std::wstring_convert<convert_type,wchar_t,std::allocator<wchar_t>,std::allocator<char>>::to_bytes(const _Elem *,const _Elem *)': cannot convert argument 1 from 'tstring' to 'wchar_t'
– RichT
Aug 10 at 18:59
@RichT tstring is not a standard C++ type. I assumed you have somewhere in your code something like: #ifdef _UNICODE #define tstring std::wstring #else #define tstring std::string #endif . What is your tstring definition?
– Amit G.
Aug 10 at 19:49
It is impossible to overload on value vs const reference.
– SergeyA
Aug 10 at 14:59