0

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

3 回答 3

7

The most likely reason why your program does not compile is that the <array> header is not compatible with pre-C11 compilers. Add

-std=c++0x

to the flags of your g++ to enable C++11 support. Once you do, you'd get a different error, because line 28 should be

cin >> (*pa)[i];

(EDITED; original answer suggested a missing reference to std::)

于 2012-11-16T22:09:06.773 回答
1

It's called std::array - it's prefixed with std:: namespace qualifier, like every standard library class or function [template].

于 2012-11-16T22:09:32.607 回答
1

add either one of the below after the #include:

using namespace std;
using std::array;
于 2012-11-16T22:09:34.537 回答