Empty an array...

Hi guys! My first post in this forum! Nice to meet you all :D!

Ok, straight to the point...

I want to empty a position of an array filled with objects of a class...

I have this code (it's just a part of the entire code)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Libreria.h
Libro * compraLibros = new Libro[2];

// Main.cpp

// AgregarLibro() is part of the Libreria class in Libreria.h
void Libreria::AgregarLibro() {
    // Book 1
    Libro * libro = new Libro;
    libro->setLibAutor("Vargas Llosa");
    libro->setLibEdicion(2010);
    libro->setLibPaginas(500);
    libro->setLibPrecio(20000);
    catalogoLibros[0] = *libro;
    delete libro;


    // Book 2
    libro = new Libro;
    libro->setLibAutor("Pablo Neruda");
    libro->setLibEdicion(1924);
    libro->setLibPaginas(500);
    libro->setLibPrecio(20000);
    catalogoLibros[1] = *libro;
    delete libro;

    libro = NULL;
}

// Then I use this method to 'Buy' those books
bool Libreria::ComprarLibro(int index, int libroComprado) {
    Libro * nLibro = &catalogoLibros[libroComprado];
    if(nLibro != NULL) {
        compraLibros[index] = * nLibro;
    } else {
        return false;
    }
    return true;
}


// And finally this method to 'return' a book to the bookstore
bool Libreria::DevolverLibro(int libroDevuelto) {
    if(&compraLibros[libroDevuelto] != NULL) {
        compraLibros[libroDevuelto] = NULL;
    }
    return true;
}


Well... I skipped a lot of code, since the problem is just in the DevolverLibro method (this method should 'return' a book, in other words, it should delete the specified book from the compraLibros Array).

Actually, the only one method that fails is DevolverLibro.

This is the order of the execution of methods :


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Libreria object
Libreria * libreria = new Libreria;


// Main.cpp

// First, I add a couple of books manually :
libreria->AgregarLibro();

// I buy both of those books (so I can delete one of them later)
// The quantity of books to buy, are asked to the user by the console, but
// to show how it works, I'll pass the values directly
libreria->ComprarLibro(0, 0); // returns true, it is a boolean method
libreria->comprarLibro(1, 1); // also returns true...

// Then I try to return (delete) on of those books from the comprarLibros array
libreria->DevolverLibro(0); // Try to delete the book in the 0 index of the compraLibros array...

// Finally, I relese the memory used by 'libreria'
delete libreria;


And the ugly part, the error message (I almost forget it)

1
2
3
4
5
main.cpp: In member function `bool Libreria::DevolverLibro(int)':
main.cpp:86: error: no match for 'operator=' in '*(((Libreria*)this)->Libreria::compraLibros + (+(((unsigned int)libroDevuelto) * 16u))) = 0'
make[2]: Leaving directory `/c/path/path/.../Libreria'
Libro.h:13: note: candidates are: Libro& Libro::operator=(const Libro&)
make[2]: *** [build/Debug/MinGW-Windows/main.o] Error 1


I'm using NetBeans 6.9 with MingW...

Ok... I can't be more specific than that... my english is limited, so if you need more info, just ask for it...

I hope you understand me... thanks in advance :).
I'll simplify your problem.
int array[2] = {5, 2};an array of integers (for simplicity. it really doesn't matter).
also note that you don't have to allocate your arrays dynamically.
it is filled with some values.

choose one:

bool flags[2] = {1, 1};array of booleans to mark whether the element in array exists or not.
if you want to remove n'th element, write flags[n] = 0; when you put it back, flags[n] = 1; don't forget to check these flags in your functions. for example
1
2
3
void print_books(){
   for(int i = 0; i < 2; i++) if(flags[i]) std::cout << array[i];
}


another one is not to use an array but one of stl containers. see http://www.cplusplus.com/reference/stl/vector/erase/
I think I got the idea about the flags...

But how to know if an element of my array is not empty? Because I'm storing objects in the array, but this piece of code

 
&array[index] != NULL


will always be true? isn't? Because I'm checking if the address in the memory the array is using is not null, so it should be true, because that address in the memory exists, am I right?


If I got this code

1
2
3
4
5
6
7
Book * book = new Book;
book->something(value);
delete book;

Book * arrayBooks = new Book[X];
arrayBooks[0] = * book;
delete [] arrayBooks;


In that case, I'm storing in arrayBooks[0] the value of the object book, right?

Anyway, I will check the vector class.

I'm just learning about C++, so is good to know all the possibilities to do what I want to do.
Please tell me that you are using polymorphism (there are subclasses of Libro)

1
2
Libro * compraLibros = new Libro[2]; //an array of Libro
compraLibros[libroDevuelto] = NULL; //now you are treat them as pointers 
Last edited on
Chuncho wrote:
But how to know if an element of my array is not empty?
hamsterman wrote:
array of booleans to mark whether the element in array exists or not



&array[index] != NULL true.

1
2
3
4
5
6
7
Book * book = new Book;
book->something(value);
delete book; //the book was destroyed

Book * arrayBooks = new Book[X];
arrayBooks[0] = * book; //Kaput
delete [] arrayBooks;
Hahaha... my mistake, sorry...


1
2
3
4
5
6
Book * arrayBooks = new Book[X];
Book * book = new Book;
book->something(value);
arrayBooks[0] = * book;
delete book;
delete [] arrayBooks;


That should work, isn't?

Now, I'm going to be more specific with my last question...

I understand the idea about the flags to mark if an element exists in the array or it doesn't. But... how?

This is what I understand about the flags...

1
2
3
if(booksArray[index] != NULL) {
     booleanFlagsArray[2] = {index, 1}; 
}


Is that right? But the boolean flags depends on booksArray.

Well... maybe I'm confusing me myself... I'll try vectors.

Thanks both. :)
1
2
3
4
5
6
7
8
9
if( booleanFlagsArray[index] ){//the object exists
//operate with the object
}

//erase an object
booleanFlagsArray[index] = false;
//add an object
booleanFlagsArray[index] = true;
booksArray[index] = some_book_object;

Topic archived. No new replies allowed.