Sam
Sam

Reputation: 485

array was not declared in this scope

I am written a c++ program and in it used array object and included <array> header file but when I try to compile it using g++ it return a lot of error messages saying that "array was not declared in this scope". What can be wrong with it. Program is here:

#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;
}

Upvotes: 0

Views: 18007

Answers (3)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726779

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::)

Upvotes: 7

Michael Shmalko
Michael Shmalko

Reputation: 710

add either one of the below after the #include:

using namespace std;
using std::array;

Upvotes: 2

Griwes
Griwes

Reputation: 9029

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

Upvotes: 1

Related Questions