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?