无法打印字符串数组元素 [英] Can't Print String Array Element
本文介绍了无法打印字符串数组元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
每当我尝试运行此程序时,它会返回一个错误:
Whenever I try to run this program it returns an error saying:
无操作符<匹配这些操作数
no operator "<<" matches these operands
另请注意,程序只在 getChoice c $ c> function。
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> #include< string>
You need to #include <string>
并且您的计算 numOfChoices
getChoice()
是错误的,因为参数 inChoices
实际上是一个指向字符串的指针,而不是 。
And your calculation of numOfChoices
in getChoice()
is wrong, since the parameter inChoices
is actually a "pointer to string" instead of "array of strings".
这篇关于无法打印字符串数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文