This is what I have tried
In the prototyping of the function
void display(const std::vector<movies>& v);
In the function definition
void display(const movies std::vector<movies>& v){
std::cout<<v.get_name();
}
The function call
display(list1);
When I run this i get These error
movies.cpp:52:46: error: expected unqualified-id before ‘&’ token
void display(const movies std::vector<movies>& v){
^
movies.cpp:52:46: error: expected ‘)’ before ‘&’ token
movies.cpp:52:48: error: expected initializer before ‘v’
void display(const movies std::vector<movies>& v){
^
Edit After suggestion from @aschepler in the comments. If i remove extra "movies" from function definition. Now the function definition becomes
void display(const std::vector<movies>& v){
std::cout<<v.get_name();
}
Still it won't compile and now It shows some different error message.
movies.cpp: In function ‘void display(const std::vector<movies>&)’:
movies.cpp:53:16: error: ‘const class std::vector<movies>’ has no
member named ‘get_name’ std::cout<<v.get_name();
^~~~~~~~~~
My full Code Is also given below if you want to go through it.
Also I m a beginner so please be kind.
definition of my class movies
movies.h file
#pragma once
#include<string>
#include<vector>
class movies{
private:
std::string name;
double rating;
int times_watched;
public:
static int num_movies;
movies();
~movies();
movies(std::string,double,int);
void set_name(std::string);
void set_rating(double);
void set_times_watched(int);
std::string get_name() const;
double get_rating() const;
int get_watch_history() const;
};
void display(const std::vector<movies>& v);
Implementation of the class
movies.cpp file
#include "movies.h"
#include<iostream>
int movies::num_movies=0;
movies::movies()
:name{"null"},rating{0},times_watched{0}{
num_movies++;
}
movies::~movies(){
num_movies--;
}
void movies::set_times_watched(int t){
times_watched=t;
}
void movies::set_name(std::string nm){
this->name=nm;
}
void movies::set_rating(double d){
this->rating =d;
}
std::string movies::get_name() const{
return this->name;
}
double movies::get_rating() const{
return this->rating;
}
int movies::get_watch_history() const{
return this->times_watched;
}
movies::movies(std::string na,double k,int s)
:name{na},rating{k},times_watched{s}{
num_movies++;
}
bool rating_check(double rt){
if((rt<=5)&&(rt>=0)){
return true;
}else{
return false;
}
}
bool times_watched_check(int watched){
if(watched>=0){
return true;
}else{
return false;
}
}
void display(const movies std::vector<movies>& v){
std::cout<<v.get_name();
}
The main program is
main.cpp file
#include<iostream>
#include<vector>
#include<string>
#include "movies.h"
int main(){
std::vector<movies>list1 (1,movies());
std::string temp {"null"},temp1;
int num=0,flag=0;
double temp2;
while(temp != "n"){
std::cout<<"enter name:";
std::cin>>temp1;
if(num!=0){
for(int j=0;j<=num;j++){
if(temp1==list1[j].get_name()){
std::cout<<"movie already present"<<std::endl;
flag=1;
break;
}
}
}
if(flag==0){
list1[num].set_name(temp1);
temp1.clear();
rating:
std::cout<<"enter your rating of movie(on a scale of 5):";
std::cin>>temp2;
if(rating_check(temp2)){
list1[num].set_rating(temp2);
}else{
std::cout<<"Invalid Input.Please Try Again."<<std::endl;
goto rating;
}
times_watched:
std::cout<<"Number of times you have watched "<<list1[num].get_name()<<":";
std::cin>>temp2;
if(times_watched_check(temp2)){
if(temp2==0){
std::cout<<"It means,You haven't Watched the movie.\nSo please first watch the movie."<<std::endl;
list1.erase(list1.begin()+num);
goto mv;
}else{
list1[num].set_times_watched(temp2);
}
}else{
std::cout<<"Invalid Input.Please Try Again."<<std::endl;
goto times_watched;
}
num++;
std::cout<<"you want to add more movies:";
std::cin>>temp;
if((temp=="Yes")||(temp=="yes")||(temp=="YES")||(temp=="y")||(temp=="Y")){
list1.resize(list1.size()+1);
continue;
}
}
if(flag==1){
mv:
std::cout<<"do you want to add any other movie:";
std::cin>>temp;
if((temp=="Yes")||(temp=="yes")||(temp=="YES")||(temp=="y")||(temp=="Y")){
list1.resize(list1.size()+1);
flag=0;
continue;
}else{
if((temp=="no")||(temp=="NO")||(temp=="n")){
break;
}else{
std::cout<<"Invalid Input.Please Try Again."<<std::endl;
goto mv;
}
}
}
}
display(list1);
return 0;
}
Aucun commentaire:
Enregistrer un commentaire