无法打印字符串数组元素

时间:2014-02-09 07:08:48

标签: c++

每当我尝试运行此程序时,都会返回错误说明:

  

无操作员“<<”匹配这些操作数

另请注意,该程序仅在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;
}

2 个答案:

答案 0 :(得分:1)

您还需要包含string标题:

#include <string>

答案 1 :(得分:1)

您需要#include <string>

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