Hello everybody, i really need a help, i was trying to create a db of books, with a class book and a list that contains the books, i did all the stuff needed but i can't figure out why the function i created to add a book to the list gives me an error, can you help me? Really thank you.
The function is "add_book", also the program is not finished yet, i need to add the last things to the main.
Here is the code:
i did all the stuff needed but i can't figure out why the function i created to add a book to the list gives me an error, can you help me?
Generally, if you get an error, you want to describe it. If it's a compilation error, tender the actual message. If your get a segmentation fault, say you get a segmentation fault. Do not say: "This doesn't work. Help!"
You never allocate memory anywhere. That's what containers like a list do. They manage memory.
1 2 3
if (head == NULL) {
head->libro = addedBook;
}
If head is NULL that means it doesn't point to a valid object. So, after you confirm that head doesn't point to a valid object, you want to access a member of the object? Seems a little backwards.
Sorry, i forgot to describe the error, my bad. It's a segmentation one, and you are right, that if is a bit weird, how can i rewrite it in the correct way? Sorry i'm learning C++ language at school and i am not so good yet.
> i m trying to make it showed like a code, but it doesnt work.
[code]code goes here[/code]
> how can i rewrite it in the correct way?
I would suggest to use a header cell, not a pointer, make your list circular and use a `last' pointer initialized to `head' address.
If you prefer to keep `head' as a pointer and end with NULL
1 2 3 4 5 6
void lista::add_book (boook b){
if(head==NULL)
head = new node(b);
elsethis->get_last()->next = new node(b);
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
class book
{
public:
char* titolo; //loading the gun
char* autore;
float prezzo;
longint data;
book ();
} addedBook;
cout << "Titolo (. per finire): ";
cin >> c;
for (int i = 0; i < c.size (); i++)
{
title[i] = c.c_str ()[i]; //pointing at your foott
}
you know about std::string, ¿why don't you use it?