【问题标题】:cpp|7|error: no matching function for call to 'std::__cxx11::basic_string<char>::basic_string(int&, int&, int&)'|cpp|7|error: no matching function for call to 'std::__cxx11::basic_string<char>::basic_string(int&, int&, int&)'|
【发布时间】:2022-01-13 15:44:09
【问题描述】:

****`我是编程新手,无法理解以下问题

所以谁能解释我如何解决这个错误。这只是一个计算第二大的简单程序`****


这是错误代码 s PC\Documents\n1\2.cpp|7|error: no matching function for call to 'std::__cxx11::basic_string::basic_string(int&, int&, int&)'|

#include<iostream>
using namespace std;

int num1, num2, num3;


string SecondGreatest(num1, num2, num3){

    string answer1;

 if(num1 > num2)
{
    if(num2 > num3)
{

        answer1 = "Num 2";
    }
    else
{

        answer1 = "Num 3";
    }
}
else if(num2 < num1)
{
    if(num1 > num3)
{

        answer1 = "Num 1";
    }else
{

    answer1 = "Num 3";
    }
}
else if(num3 > num2)
{
    if(num2 > num1)
{

        answer1 = "Num 2";
    }else
{

        answer1 = "Num 1";
    }
}
else if(num3 > num1)
{
    if(num1 > num2)
{

        answer1 = "Num 1";
    }else
{

    answer1 = "Num 2";
    }
}
 return answer1;
 }

//main function
 int main(){

     cout << "Write any three numbers" << endl;
     cin >> num1;
     cin >> num2;
     cin >> num3;
//outputting the second greatest function
 cout << SecondGreatest(num1, num2, num3) << endl;



 return 0;
 }

【问题讨论】:

  • 尝试将 string SecondGreatest(num1, num2, num3) 更改为 string SecondGreatest(int num1, int num2, int num3) 并且您不应为参数和全局变量使用相同的名称,并且 num1num2num3 不需要是全局的.
  • 另外,我不认为cout&lt;&lt; 运算符对string 有重载。

标签: c++11


【解决方案1】:

请按如下所示更新您的代码,并考虑在固定代码中以 cmets 形式给出的提示:

#include <iostream>
#include <string>     // Don't forget to include std::string header
using namespace std;  // OK for small code examples, but should be avoided in any real C++ code

int num1, num2, num3; // No need to define this in global scope. Could be moved to the main function.

string SecondGreatest(int num1, int num2, int num3) // Variable type required for each parameter, here int
{
    // Please care about the indentation. It makes your code better readable.
    // Logic improved to get always the second greatest number given.
    // num1 is the greatest number, now let us find the second greatest.
    if(num1 > num2 && num1 > num3 && num2 != num3)
    {
        if(num2 > num3)
        {
            return "Num 2";
        }
        else
        {
            return "Num 3";
        }
    }
    // num2 is the greatest number, now let us find the second greatest.
    else if(num2 > num1 && num2 > num3 && num1 != num3)
    {
        if(num1 > num3)
        {
            return "Num 1";
        }
        else
        {
            return "Num 3";
        }
    }
    // num3 is the greatest number, now let us find the second greatest.
    else if(num3 > num1 && num3 > num2 && num1 != num2)
    {
        if(num2 > num1)
        {
            return "Num 2";
        }
        else
        {
            return "Num 1";
        }
    }
    else
    {
        return "Some or all of the given numbers are equal.";
    }
}

//main function
int main()
{
    cout << "Write any three numbers" << endl;
    cin >> num1;
    cin >> num2;
    cin >> num3;
    //outputting the second greatest function
    cout << SecondGreatest(num1, num2, num3) << endl;
    return 0;
}

使用 Visual Studio 2019 测试,因此使用 Visual C++ 2019 编译器。

您的错误可能会发生,因为您的函数SecondGreatest(...) 的定义中缺少参数类型int。但还必须包含 string 标头才能使您的程序运行。

还请注意查找给定三的第二大数字并正确处理代码缩进的逻辑,因为它使代码更具可读性。

【讨论】:

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