Can't Print String Array Element [closed]

2020-05-06 10:34发布

Whenever I try to run this program it returns an error saying:

no operator "<<" matches these operands

Also note that the program only runs into this problem in the getChoice() function.

#include <iostream>
#include "utilities.h"

using namespace std;

int getChoice(string inChoices[]){
    int numOfChoices = sizeof(inChoices) / sizeof(inChoices[0]);
    string x = inChoices[0];
    string y = inChoices[1];
    cout << x << endl << y << endl;
    return numOfChoices;
}

int main()
{
    string choices[2] = { "Happy Day", "Even Better Day" };
    cout << utilities::getChoice(choices) << endl;

    cout << endl << sizeof(choices) / sizeof(choices[0]) << endl;
}

标签: c++
2条回答
▲ chillily
2楼-- · 2020-05-06 10:52

You need to #include <string>

And your calculation of numOfChoices in getChoice() is wrong, since the parameter inChoices is actually a "pointer to string" instead of "array of strings".

查看更多
萌系小妹纸
3楼-- · 2020-05-06 11:06

You need also to include the string header:

#include <string>
查看更多
登录 后发表回答