2016-05-03 28 views
-1

このネストされた構造体配列を初期化しようとしていますが、私のコンパイラは初期化子の値が多すぎると伝え続けます。私はこれについて行くことができる別の方法はありますか?誰かが私が間違っていることを教えてくれるかもしれません。ネストされた構造体配列を初期化する

#include<iostream> 
#include<iomanip> 
#include<string> 
using namespace std; 

const int NUM_BOOKS = 3; 

struct BookInfo 
{ 
    string title; 
    double price; 
}; 

struct Author 
{ 
    string name; 
    BookInfo books[NUM_BOOKS]; 
}; 

//Prototype 
void showInfo(Author a[], int); 


int main() 
{ 
    Author a[] = 
    { 
    { "NONE", { "NONE", 0 }, { "NONE", 0 }, { "NONE", 0 } }, 
    { "NONE", { "NONE", 0 }, { "NONE", 0 }, { "NONE", 0 } }, 

    { "NONE", { "NONE", 0 }, { "NONE", 0 }, { "NONE", 0 } } 
}; 

cout << fixed << showpoint << setprecision(2); 
showInfo(a, NUM_BOOKS); 

return 0; 
} 

//This fucnction displays the array 
void showInfo(Author a[], int size) 
{ 
    for (int i = 0; i< size; i++) 
{ 
    cout << "The author: " << a[i].name << endl; 
    for (int j = 0; j < size; j++) 
    { 
     cout << "\t The title: " << a[i].books[j].title << ", "; 
     cout << "\t The price: " << a[i].books[j].price << endl; 
    } 

} 

cout << endl; 

} 

答えて

4

アレイは、オブジェクトもあるので、あなたはそれに接するために} {を使用する必要があります。

Author a[] = 
{ 
{ "NONE", {{ "NONE", 0 }, { "NONE", 0 }, { "NONE", 0 }} }, 
{ "NONE", {{ "NONE", 0 }, { "NONE", 0 }, { "NONE", 0 }} }, 
{ "NONE", {{ "NONE", 0 }, { "NONE", 0 }, { "NONE", 0 }} } 
}; 
関連する問題