“NewString::NewString”: 没有重载函数可以转换所有参数类型
时间: 2024-01-10 18:02:34 浏览: 46
这个错误通常是由于函数调用时传递的参数类型与函数定义的参数类型不匹配导致的。编译器无法找到合适的重载函数来处理传递的参数,因此产生了这个错误。
为了解决这个问题,你需要检查函数的定义和调用的参数类型是否匹配。如果不匹配,你可以修改函数的定义或者调用的参数类型,使其匹配。另外,你还需要确保函数的定义和声明之间的参数类型也是一致的。
例如,如果你有如下的函数定义:
```cpp
void NewString(int i, std::string s);
```
但是在调用该函数时传递的参数类型与定义的参数类型不匹配,例如:
```cpp
NewString("hello", 123);
```
那么编译器就会报出上述的错误。你需要修改调用函数的参数类型,使其与定义的参数类型匹配,例如:
```cpp
NewString(123, "hello");
```
或者你可以重载函数,提供更多的参数类型支持。
相关问题
rror C2661: “std::locale::facet::operator new”: 没有重载函数接受 3 个参数
这个错误通常是因为编译器版本不同,导致使用的语言库版本不同所致。解决方法是使用更加通用的方式进行编码转换。
示例一:将字符串存入UTF-8格式的xml文件A
```cpp
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
// 待存入的字符串
wstring str = L"这是一个测试";
// 打开文件
wofstream ofs("test.xml");
// 写入XML头
ofs << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
// 将wstring转换为utf8编码的字符串
string utf8str;
for (auto c : str) {
if (c <= 0x7f) {
utf8str.push_back(static_cast<char>(c));
} else if (c <= 0x7ff) {
utf8str.push_back(static_cast<char>((c >> 6) | 0xc0));
utf8str.push_back(static_cast<char>((c & 0x3f) | 0x80));
} else if (c <= 0xffff) {
utf8str.push_back(static_cast<char>((c >> 12) | 0xe0));
utf8str.push_back(static_cast<char>(((c >> 6) & 0x3f) | 0x80));
utf8str.push_back(static_cast<char>((c & 0x3f) | 0x80));
} else if (c <= 0x10ffff) {
utf8str.push_back(static_cast<char>((c >> 18) | 0xf0));
utf8str.push_back(static_cast<char>(((c >> 12) & 0x3f) | 0x80));
utf8str.push_back(static_cast<char>(((c >> 6) & 0x3f) | 0x80));
utf8str.push_back(static_cast<char>((c & 0x3f) | 0x80));
}
}
// 写入字符串
ofs << "<root>" << utf8str << "</root>";
// 关闭文件
ofs.close();
return 0;
}
```
示例二:从xml文件A读取出字符串并显示在MFC界面上
```cpp
#include <iostream>
#include <fstream>
#include <string>
#include <afx.h>
using namespace std;
int main()
{
// 打开文件
wifstream ifs("test.xml");
// 读取文件内容到字符串中
wstring str;
getline(ifs, str, static_cast<wchar_t>(EOF));
// 关闭文件
ifs.close();
// 将utf8编码的字符串转换为wstring
wstring wstr;
for (size_t i = 0; i < str.size(); ++i) {
wchar_t c = str[i];
if ((c & 0x80) == 0x00) {
wstr.push_back(c);
} else if ((c & 0xe0) == 0xc0) {
wstr.push_back(((c & 0x1f) << 6) | (str[++i] & 0x3f));
} else if ((c & 0xf0) == 0xe0) {
wstr.push_back(((c & 0x0f) << 12) | ((str[++i] & 0x3f) << 6) | (str[++i] & 0x3f));
} else if ((c & 0xf8) == 0xf0) {
wstr.push_back(((c & 0x07) << 18) | ((str[++i] & 0x3f) << 12) | ((str[++i] & 0x3f) << 6) | (str[++i] & 0x3f));
}
}
// 显示字符串
AfxMessageBox(wstr.c_str());
return 0;
}
```
这两个示例分别实现了将wstring类型的字符串转换为utf8编码的字符串,以及将utf8编码的字符串转换为wstring类型的字符串。这样就可以在不使用locale库的情况下实现字符串的编码转换。
C++ new string
在C++中,创建一个新的字符串可以使用`std::string`类。你可以在代码中使用以下方式来创建一个新的字符串对象:
```cpp
#include <string>
int main() {
std::string str = "Hello, World!"; // 创建一个新的字符串对象并初始化
return 0;
}
```
在上面的示例中,我们使用了`std::string`类并将其初始化为"Hello, World!"。这将创建一个新的字符串对象并将其赋值给变量`str`。
你可以按照需要更改字符串的内容,例如添加、删除或修改其字符。`std::string`类提供了许多用于操作字符串的成员函数和操作符重载,以便于对字符串进行操作。
希望这可以回答你的问题!如果你有任何进一步的问题,请随时提问。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)