无法将参数'2'的'std :: string {aka std :: basic_string <char>}'转换为'int'为'std :: string

时间:2018-01-16 04:40:06

标签: c++ arrays string

我试图按字母顺序获取最小值。

这是我到目前为止编写的代码:

#include <iostream>
#include <string>
using namespace std;

void swap(string[], int i, int j);
string displayArray(string[], int size);
string findMin(string[] , int size);

int main()
{
    string themin;
    int size;
    string A[7] = { "MBA", "ME", "Psy", "EE", "CS", "CE", "Bio" };
    for (int i = 0; i < 7; i++)
    {
        cout << A[i]<<" ";
    }
    cout << endl;
    swap(A, 1, 4);

    for (int i = 0; i < 7; i++)
    {
        cout << A[i] << " ";
    }
    findMin(A, themin);
    cout<< themin << endl;

}

void swap(string A[], int i, int j)
{
    string temp = A[i];
    A[i] = A[j];
    A[j] = temp;
}

string findMin(string A[] , int size)
{
    string themin = A[0];
    for(int i = 1; i < size; i ++)
    {
        if(A[i].compare(themin) < 0)
            themin=A[i];
    }
    return themin;
}

我收到此错误,但不明白如何修复它:

  

25:26:错误:无法将'std :: string {aka std :: basic_string&lt; char&gt;}'转换为'int',将参数'2'转换为'std :: string findMin(std :: string *, INT)'

1 个答案:

答案 0 :(得分:0)

替换这两行:

findMin(A, themin);
cout<< themin << endl;

使用:

themin = findMin(A, sizeof A / sizeof A[0]);
cout << endl << themin << endl;