簡體   English   中英

未在此范圍內聲明數組

[英]array was not declared in this scope

我編寫了一個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;
}

您的程序無法編譯的最可能的原因是<array>標頭與C11之前的編譯器不兼容。

-std=c++0x

到g ++的標志以啟用C ++ 11支持。 完成后,您將得到另一個錯誤 ,因為第28行應為

cin >> (*pa)[i];

(已編輯 ;原始答案表明缺少對std::引用)

它被稱為std::array它以std::命名空間限定符為前綴,就像每個標准庫類或函數[template]一樣。

在#include之后添加以下任一內容:

using namespace std;
using std::array;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM