【问题标题】:no matching function for call to 'make_pair(char [len], int&)调用 'make_pair(char [len], int&) 没有匹配的函数
【发布时间】:2018-07-03 04:45:34
【问题描述】:

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

没有匹配函数调用'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;
}

【问题讨论】:

  • 那里有一堆奇怪的东西,但make_pair 取决于编译时知识。 len 在编译时未知。
  • 考虑使用std::vector&lt;char&gt; 而不是char[len]。它是一个容器,您可以在std::make_pair 中使用它

标签: c++ arrays return std-pair


【解决方案1】:

您不能将堆栈分配的字符数组作为 const char* 返回,因为该数组在函数结束后将不再存在,并且指针将指向无效位置。

另请注意,在堆栈上创建一个可变大小的数组是非标准的 gcc 扩展。

只返回std::string 会简单得多,那么您根本不需要一对。

顺便说一句,我认为要解决 make pair 的问题,您需要将字符数组转换为 const char *

【讨论】:

    【解决方案2】:

    您可以使用std::string

    这样你可以使用std::stringsize()方法来获取它的长度。

    您还可以返回一个本地 std::string 变量,这将使用移动语义为您获取正确的返回值。

    如果您确实想使用std::pair,您仍然可以将std::string 传递给它-std::pair&lt;std::string, int&gt;,但是如果int 表示字符串长度,则不需要。

    【讨论】:

      【解决方案3】:

      坚持编译器错误,我生成以下最小示例。

      #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&lt;const char*, int&gt;

      int main() {
      
          int len = 10;
          char * ch = new char[len];
          make_pair(ch, len);
      }
      

      当您完成分配的存储时,请记住delete。这也将修复 Alan Birtles 发现的错误。注意:You could also use a smart pointer,但如果您愿意走那么远,请走四分之一,然后使用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

      这应该可以编译您的代码。剩下的就是逻辑错误了。

      【讨论】:

        猜你喜欢
        • 2011-04-03
        • 1970-01-01
        • 1970-01-01
        • 2020-07-02
        • 1970-01-01
        • 2020-07-14
        • 1970-01-01
        • 1970-01-01
        • 2018-12-05
        相关资源
        最近更新 更多