How do I get rid of the “Intellisense: no suitable conversion function from ”std::string“ to ”std::string *“ exists” error?

拥有回忆 提交于 2019-12-12 03:54:36

问题


So here's my code,

#include <iostream>
#include <string>

using namespace std;

int main()
{
const int NUM_NAMES = 20;
string names [NUM_NAMES] = {"Collins, Bill", "Smith, Bart" , "Allen, Jim", "Griffin, Jim", "Stamey, Marty", "Rose, Geri","Taylor, Terri",
    "Johnson, Jill", "Allison, Jeff", "Looney, Joe" , "Wolfe, Bill", "Rutherford, Greg", "Javens, Renee","Harrison, Rose","Setzer, Cathy",
    "Pike, Gordon","Holland, Beth"};
string smallest;
int minindex;
void displayNames (string[NUM_NAMES], int);



cout<<"Here are the unalphabetized names";
****displayNames(names[NUM_NAMES], NUM_NAMES);****

for(int i=0; i < (NUM_NAMES -1); i++)
{
    minindex=i;
    smallest=names[i];
    for(int j = (i+1); j<NUM_NAMES; j++)
    {
        if(names[j]<smallest)
        {
            smallest = names[j];
            minindex = j;
        }
    }
    names[minindex] =names[i];
    names[i] = smallest;

    system("pause");
    return 0;
}
}

void displayNames(string names[], int NUM_NAMES)
{
    for (int i =0; i<NUM_NAMES; i ++)
{
    cout<<names[i]<<endl;
}
}

The line bracketed by four asterisks is the line that the error code references when I try to build. The asterisks are NOT there in my actual code. I don't understand how to fix the error

Intellisense: no suitable conversion function from "std::string" to "std::string *" exists

I've gone through pages and pages of searches, however all the questions/answers appear to refer to a conversion function from string to int or string to char etc. not string to string.

On a side note, I do also have to alphabetize these, will what I currently have work? My professor was saying that the computer should evaluate the strings' char values.


回答1:


This is wrong:

displayNames(names[NUM_NAMES], NUM_NAMES);

This is the correct one:

displayNames(names, NUM_NAMES);



回答2:


You should send names to the function not names[NUM_NAMES]. names is an array or a string pointer but names[NUM_NAMES] is a string. Your function takes a string pointer.

std::String[] is identical to std::String*



来源:https://stackoverflow.com/questions/16108162/how-do-i-get-rid-of-the-intellisense-no-suitable-conversion-function-from-std

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!