作为一个C++新手,我试图在我的程序中创建一个简单的void函数来显示一个数组。然而,正如标题所示,出现了错误。我相信问题在于我尝试使用不同于函数参数形式的数组进行调用。我不确定该如何修改。
#include <iostream>
#include <vector>
using namespace std;
void display_array(string arr[]){
int i;
for (i = 0; i < sizeof(arr); i++);
cout<<arr[i];
}
int main()
{
string current;
std::vector<string> paths;
cout<<"Input paths in the form 'AB'(0 to exit)";
cin>>current;
while (current != "0"){
paths.push_back(current);
cin>>current;
}
display_array(paths);
}
非常感谢您的帮助。
display_array
应该打印一个std::vector<std::string>
,则将参数设置为const std::vector<std::string>>&
。 - Baum mit Augenfor
循环末尾有一个分号,这意味着循环除了递增i
之外什么也不做,下一行代码只会执行一次。 - sjdowling