Я пишу простую функцию UTF8 в WideStr на VC ++.
MtoW.h
#pragma once
#include <Windows.h>
#include <string>
using namespace std;
wstring UTF8toWide(const char*& in);
MtoW.cpp
#include "MtoW.h"
#include <string>
#include <cassert>
using namespace std;
wstring UTF8toWide(const char*& in) {
int size = MultiByteToWideChar(CP_UTF8, 0, in, -1, NULL, 0);
wchar_t* wide_str = new wchar_t[size];
int result_size = MultiByteToWideChar(CP_UTF8, 0, in, -1, wide_str, size);
assert(size == result_size);
return wstring(wide_str);
}
main.cpp
#include <string>
#include <iostream>
#include "MtoW.h"
using namespace std;
int main()
{
string s = "hi";
//s.c_str();
//const char* q = "abc";
//const char*& c = q;
wstring q = UTF8toWide(s.c_str()); //error
}
Я получил ошибку cannot convert from const char* to const char *&
, почему?
s.c_str()
имеет тип const char*
, и мой код должен работать.