我编写了一个 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;
}