array of a class

book.h

1
2
3
4
5
6
7
8
9
10
11
class book {
           string categoria;
           string titulo;
           string autor;
           int paginas;
           int edicao;
           int registro;
           int volume;
    public:
           book(int, string, string, int, int, int, string);
};


book.cpp

1
2
3
4
5
6
7
8
9
book::book(int reg, string tit, string aut,int vol, int edi, int pag, string cat) {
                 registro = reg;
                 titulo = tit;
                 autor = aut;
                 volume = vol;
                 edicao = edi;
                 paginas = pag;
                 categoria = cat;
}

main.cpp

1
2
3
4
5
6
7
8
9
10
int main() {
    book li[5] = {{43655, "Eletricidade", "Halliday", 1, 7, 428, "Fisica"}, 
                   {74854, "Plantas", "Sonia", 2, 3, 247, "Biologia"},
                   {48762, "Crepusculo", "Stephenie", 1, 1, "Ficcao"}, 
                   {12489, "Calculo", "Thomas", 2, 5, "Matematica"}, 
                   {34579, "C++", "Deitel", 1, 3, "Programacao"}};
                   
    getch();
    return 0;
}


erros:
45 book.cpp brace-enclosed initializer used to initialize `livro'
45 book.cpp brace-enclosed initializer used to initialize `livro'
45 book.cpp brace-enclosed initializer used to initialize `livro'
45 book.cpp brace-enclosed initializer used to initialize `livro'
45 book.cpp brace-enclosed initializer used to initialize `livro'

What's the problem with the program?
Last edited on
You can't initialize an array of objects like that.
I saw this way in a forum web site. Could you tell me how I can initialize an array of objects?
Also did you actually write the constructor? Or did you just declare it and hope for the best?
I am relatively sure you can do it like this:

1
2
3
4
5
book li[5] = {book(43655, "Eletricidade", "Halliday", 1, 7, 428, "Fisica"), 
                   book(74854, "Plantas", "Sonia", 2, 3, 247, "Biologia"),
                   book(48762, "Crepusculo", "Stephenie", 1, 1, "Ficcao"), 
                   book(12489, "Calculo", "Thomas", 2, 5, "Matematica"), 
                   book(34579, "C++", "Deitel", 1, 3, "Programacao")};
it didn't work
of course it didnĀ“t
just make a little constructor an it will work
Last edited on
i did one with only three arguments and the program still not working
@OP: Copy paste your constructor please.
the constructor is alrealdy in the topic
1) They need to be using the constructors like hanst99 stated
2) Your 3rd-5th constructors don't have enough arguments (you are missing a number)
Topic archived. No new replies allowed.