调用 'make_pair(char [len], int&) 没有匹配函数

no matching function for call to 'make_pair(char [len], int&)

本文关键字:int 函数 make pair 调用 char len      更新时间:2023-10-16

这是我使用注释的第一个代码,我知道注释并不完美。 当我尝试构建代码时,编译器给了我此错误

没有匹配函数调用 '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.firstconst char *,不是char

这应该让你的代码编译。剩下的就是逻辑错误了。