0

I'm trying to complete a challenge from my course and for some reason when I'm delegating constructors i get some errors, also when I try to initialize dynamically

#include <iostream>
#include <vector>
#include <string>

using namespace std;

class Movie
{
private:
    friend class Movies;
    string name;
    string rating;
    int times_watched;
    Movie *data;
public:
    Movie(string name1,string rating1, int times_watched1)
        :name{name1},rating{rating1},times_watched{times_watched1} {}
    Movie(Movie *data1)
    {
        data->name = data1->name;
        data->rating = data1->rating;
        data->times_watched = data1->times_watched;
    }
    Movie(const Movie &source)
        :Movie{ *source.data } {}
    Movie(Movie &&source) noexcept
        :Movie{ source.data }
    {
        source.data = nullptr;
    }
};
class Movies
{
private:
    vector <Movie> list;
public:
    bool check_movie(Movie &movie1)
    {
        for (int i = 0; i < list.size(); i++)
            if (list.at(i).name == movie1.name)
                return false;
            else return true;
    }
    void add_movie(Movie &movie1)
    {
        if (check_movie(movie1) == false)
            list.push_back(movie1);
        else cout << "Movie is already in the list\n";
    }
    void increment_watched_count(Movie &movie1)
    {
        if (check_movie(movie1) == true)
            movie1.times_watched++;
        else cout << "Movie is not on the list\n";
    }
    void display_list()
    {
        for (int i = 0; i < list.size(); i++)
            cout << list.at(i).name << "  |  " << list.at(i).rating << "  |  "
            << list.at(i).times_watched;
    }
};

int main()
{
    Movies collection;
    collection.add_movie(Movie{ "Dictator","PG-13",3 });
}

On the last line of code

collection.add_movie(Movie{ "Dictator","PG-13",3 });

I get an error "initial value of reference to non-const must be an lvalue"

Also at my deep constructor definition

Movie(const Movie &source)
        :Movie{ *source.data } {}

I get the error constructor delegates directly or indirectly to itself

How do I fix these errors?

  • 1
    Error is from that signature `void add_movie(Movie &movie1)`, parameter should be one of `Movie&&`, `Movie` or `const Movie&`. (The later would be the simplest in your case IMO) – Jarod42 Nov 10 '21 at 14:40
  • 2
    why do you have a pointer to another movie inside the movie? – bb1950328 Nov 10 '21 at 14:42
  • 1
    Assuming a reasonable answer to @bb1950328's question, you want to construct the member called `data` not the class `Movie` – Tim Randall Nov 10 '21 at 14:43
  • I was thinking i could allocate dynamically more efficient like this using the move constructor – Ioan Săsărman Nov 10 '21 at 14:45
  • Change the signatures for `void add_movie(const Movie &movie1)` and `bool Movies::check_movie(const Movie& movie1)` – Pierre Nov 10 '21 at 14:45
  • *"I get an error **'initial value of reference to non-const must be an lvalue'**"* -- yes, that is what your code says to do. For better help, your question should describe what that constructor is intended to do. How do you see this not resulting in infinite recursion as `Movie(const Movie &source)` invokes itself to construct the object? – JaMiT Nov 10 '21 at 14:46
  • what about the constructor error? – Ioan Săsărman Nov 10 '21 at 14:47
  • um, the move is *extremely* cheap, or often actually free. Dynamic allocation is pretty much always expensive. – Marcus Müller Nov 10 '21 at 14:47
  • [OT]: `check_movie` (Which has 2 missing `const`) has logical error. it only works if `list` has exactly one element. – Jarod42 Nov 10 '21 at 14:51
  • @IoanSăsărman *"what about the constructor error?"* -- you have just experienced one of the drawbacks of asking multiple questions in one post. Independent errors warrant independent questions. *(One quick way to see that your errors are independent is to comment out the `collection.add_movie` line. That gets rid of one error but not the other, right? If so, the error that remains is not dependent on the one that disappeared, so the one that remains can get its own question.)* – JaMiT Nov 10 '21 at 15:01
  • I just fixed the issue by removing pointers all togheter and it worked :) – Ioan Săsărman Nov 10 '21 at 15:04
  • and a few of the constructors – Ioan Săsărman Nov 10 '21 at 15:04

0 Answers0