我写了一个c ++程序,并在其中使用了array对象和包含<array>头文件,但当我尝试使用g++它编译它时返回很多错误消息,说"数组未在此范围内声明".它可能有什么问题.计划在这里:
#include <iostream>
#include <string>
#include <array>
using namespace std;
const int Seasons = 4;
const array<string, Seasons> Snames =
{"spring", "summer", "fall", "winter"};
void fill(array<double, Seasons>* pa);
void show(array<double, Seasons> da);
int main()
{
array<double, Seasons> expenses;
fill(&expenses);
show(expenses);
return 0;
}
void fill(array<double, Seasons>* pa)
{
for(int i = 0; i < Seasons; i++)
{
cout << "Enter " << Snames[i] << " expenses: ";
cin >> *pa[i];
}
}
void show(array<double, Seasons> da)
{
double total = 0.0;
cout << "\nEXPENSES\n";
for(int i = 0; i < Seasons; i++)
{
cout << Snames[i] << ": $" << da[i] << endl;
total += da[i];
}
cout << "Total Expenses: $" << total << endl;
}
Run Code Online (Sandbox Code Playgroud)
程序无法编译的最可能原因是<array>标题与C11之前的编译器不兼容.加
-std=c++0x
Run Code Online (Sandbox Code Playgroud)
到你的g ++的标志,以启用C++ 11支持.一旦你这样做,你会得到一个不同的错误,因为第28行应该是
cin >> (*pa)[i];
Run Code Online (Sandbox Code Playgroud)
(已编辑 ;原始答案建议缺少参考std::)