无法打印字符串数组元素

Can't Print String Array Element

本文关键字:数组元素 字符串 打印      更新时间:2023-10-16

每当我尝试运行这个程序时,它都会返回一个错误,说:

没有运算符"<<"与这些操作数匹配

另请注意,程序仅在getChoice()函数中遇到此问题。

#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;
}

还需要包含string标头:

#include <string>

你需要#include <string>

而且您在getChoice()中对numOfChoices的计算是错误的,因为参数 inChoices 实际上是"指向字符串的指针"而不是"字符串数组"。