2016-05-03 12 views
-1

Ich versuche, diese verschachtelte Struktur-Array zu initialisieren und mein Compiler sagt mir, dass ich zu viele Initializer-Werte habe. Gibt es eine andere Möglichkeit, das zu tun? oder könnte mir jemand sagen, was ich falsch mache.Initialisierung eines verschachtelten Struktur-Arrays

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

} 

Antwort

4

Array ist auch Objekt, so dass Sie verwenden müssen {} es zur Grenze:

Author a[] = 
{ 
{ "NONE", {{ "NONE", 0 }, { "NONE", 0 }, { "NONE", 0 }} }, 
{ "NONE", {{ "NONE", 0 }, { "NONE", 0 }, { "NONE", 0 }} }, 
{ "NONE", {{ "NONE", 0 }, { "NONE", 0 }, { "NONE", 0 }} } 
};