Difficulties on atributting a array to another [closed]

You can’t copy an array with operator=. You can fix your problem using modern C++

#include <array>
#include <cstddef>
// #include <list>
#include <string>
// #include <vector>

struct book{
   std::string author;
   std::string title;
};
std::array<book, 100> library; // or std::vector<book> library; 
                               // or std::list<book> library;

void removebook(std::size_t idx) {
    for (std::size_t cont = idx; cont < library.size() - 1; ++cont){
        std::size_t cont2 = cont + 1;
        library

You can't copy an array with operator=. You can fix your problem using modern C++

#include <array>
#include <cstddef>
// #include <list>
#include <string>
// #include <vector>

struct book{
   std::string author;
   std::string title;
};
std::array<book, 100> library; // or std::vector<book> library; 
                               // or std::list<book> library;

void removebook(std::size_t idx) {
    for (std::size_t cont = idx; cont < library.size() - 1; ++cont){
        std::size_t cont2 = cont + 1;
        library[cont] = library[cont2];
    }
}

Probably you can replace the body of removebook with a function from algorithm like std::remove or a method like std::vector::erase or std::list::erase depending on how you implement it, e.g. for std::array<book, 100> library

#include <array>
#include <cstddef>
#include <string>

struct book{
   std::string author;
   std::string title;
};
std::array<book, 100> library;

void removebook(std::size_t idx) {
    std::copy(library.begin() + idx + 1, library.end(), library.begin() + idx);
    library.back() = book{};
}

= library[cont2];
}
}

Probably you can replace the body of removebook with a function from algorithm like std::remove or a method like std::vector::erase or std::list::erase depending on how you implement it, e.g. for std::array<book, 100> library

#include <array>
#include <cstddef>
#include <string>

struct book{
   std::string author;
   std::string title;
};
std::array<book, 100> library;

void removebook(std::size_t idx) {
    std::copy(library.begin() + idx + 1, library.end(), library.begin() + idx);
    library.back() = book{};
}

Leave a Comment