调用 'make_pair(char [len], int&) 没有匹配函数
no matching function for call to 'make_pair(char [len], int&)
这是我使用注释的第一个代码,我知道注释并不完美。 当我尝试构建代码时,编译器给了我此错误
没有匹配函数调用 'make_pair(char [len], int&(。
我想返回两个带有std::pair
变量
请注意:我只想调试这段代码,所以不要在第一阶段编写自己的方法来做我想做的事情。
我使用 gcc 5.1,我的操作系统是 Windows
/* this program takes an string from user and output its morse code equivalent */
#include <iostream>
#include <cstring>
#include <algorithm>
#include <utility>
using namespace std;
/* getString() is for getting a text with type std::string and converse all the letters in it
to lower case in order to switch case then converse std::string text type to cstring to be able to loop through it with
for loop*/
pair<const char*, int> getString()
{
string a;
getline(cin, a);
// converse all the letters in string a to lower case for switch case
transform(a.begin(), a.end(), a.begin(), ::tolower);
int len = a.length() + 1;
char ch[len];
strcpy(ch, a.c_str());
return make_pair(ch, len); //this line causing error
}
int main() {
p = pair<const char*, int> getString();
char ch = p.first;
int len = p.second;
string morseCode;
/*this for loop search in the ch character array and add morse code equivalent of each letter to morseCode
string Variab*/
for (int i = 0; i < len; i++)
switch(ch[i])
{
case ' ':
morseCode += "/ ";
break;
case 'a':
morseCode += ".- ";
break;
case 'b':
morseCode += "-... ";
break;
case 'c':
morseCode += "-.-. ";
break;
case 'd':
morseCode += "-.. ";
break;
case 'e':
morseCode += ". ";
break;
case 'f':
morseCode += "..-. ";
break;
case 'g':
morseCode += "--. ";
break;
case 'h':
morseCode += ".... ";
break;
case 'i':
morseCode += ".. ";
break;
case 'j':
morseCode += ".--- ";
break;
case 'k':
morseCode += "-.- ";
break;
case 'l':
morseCode += ".-.. ";
break;
case 'm':
morseCode += "-- ";
break;
case 'n':
morseCode += "-. ";
break;
case 'o':
morseCode += "--- ";
break;
case 'p':
morseCode += ".--. ";
break;
case 'q':
morseCode += "--.- ";
break;
case 'r':
morseCode += ".-. ";
break;
case 's':
morseCode += "... ";
break;
case 't':
morseCode += "- ";
break;
case 'u':
morseCode += "..- ";
break;
case 'v':
morseCode += "...- ";
break;
case 'w':
morseCode += ".-- ";
break;
case 'x':
morseCode += "-..- ";
break;
case 'y':
morseCode += "-.-- ";
break;
case 'z':
morseCode += "--.. ";
break;
}
cout << morseCode;
return 0;
}
不能将堆栈分配的字符数组作为const char*
返回,因为该数组在函数结束后将不再存在,并且指针将指向无效位置。
另请注意,在堆栈上创建大小可变的数组是非标准的 gcc 扩展。
返回会简单得多std::string
然后你根本不需要一对。
另外,我认为要解决 make 对的问题,您需要将字符数组转换为const char *
您可以使用std::string
.
这样,您可以使用size()
std::string
方法来获取其长度。
您还可以返回局部std::string
变量,这将使用 move 语义来获取正确的返回值。
如果你确实想使用std::pair
你仍然可以传入一个std::string
-std::pair<std::string, int>
,但是如果int
表示字符串长度,则不需要它。
坚持编译器错误,我生成以下最小示例。
#include <algorithm>
#include <utility>
using namespace std;
int main() {
int len = 10;
char ch[len];
make_pair(ch, len); //this line causing error
}
make_pair
是一个函数模板,它将在编译时创建一个基于变量len
的函数,该变量仅在运行时已知。这源于char ch[len];
是一个可变长度数组,由于这样的问题,这在标准C++中是非法的。你应该看到可变长度数组之后sizeof
的恐怖,但这已经跑题了。
最好的解决方案是始终坚持std::string
,但要保持生产pair<const char*, int>
,
int main() {
int len = 10;
char * ch = new char[len];
make_pair(ch, len);
}
完成后,请记住delete
分配的存储。这也将修复Alan Birtles发现的错误。注意:您也可以使用智能指针,但如果您愿意走那么远,请走四分之一的距离,只需使用std::string
。
补遗
在
p = pair<const char*, int> getString();
p
需要一个类型。当前该类型位于错误的位置
pair<const char*, int> p = getString();
这也是使用auto
的好地方。
auto p = getString();
下一个
char ch = p.first;
p.first
是const char *
,不是char
。
这应该让你的代码编译。剩下的就是逻辑错误了。
- 调用'begin(int [n])'没有匹配函数
- 在函数中使用 const int size 参数创建数组会在 Visual Studio 中抛出错误 C++:表达式的计
- 为什么 'main' 函数中的局部 int 变量会自动初始化?
- "Missing type specifier - int assumed"无法通过向主函数添加"return 0"来解决
- 为什么 c++ 中的 main() 函数不采用除 int 和 void 之外的任何其他返回类型
- swap(int&, int&) 函数不起作用,当交换不使用临时变量时?
- 如何在不返回 C++ 值的情况下退出 int 函数?
- 调用 Parse int 函数时编译 parse 整数程序时出现问题
- 如何将字符串返回到主INT函数
- 为什么要使用int函数而不是void
- int 函数C++可以返回类似 "NULL" 的东西?
- C++int函数.当零是有效的返回值时,我如何检测false
- int const函数(参数)、int函数(const参数)和int函数(parameters)const之间的区别是什
- 类无法从主C++访问 int 函数
- 无法从 int 函数返回结果
- 如何将'int'函数调用到'void'函数
- 强制字符串为int函数以消耗整个字符串
- Int函数参数不能正常工作
- 删除Mat int函数(opencv)
- 为什么float参数适合int函数参数