help with vectors in a class file

Hi, i keep getting

error C2664: 'void std::vector<_Ty>::push_back(std::basic_string<_Elem,_Traits,_Alloc> &&)' : cannot convert parameter 1

using the code:
1
2
3
4
5
6
	void Movie::setActor(vector<string> & actors) {
		if (getTitle() == "Godzilla"){
		actors_= actors_.push_back(actors); //error in actors_.pushback()
		}
	}


I'm try to store the user inputed actor names in a vector. Can anyone tell me what this error means and how I can resolve this?

here's the .h file
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#ifndef MOVIE_
#define MOVIE_

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


using namespace std;

	enum Movie_Rating {G,PG,PG13,R,NC17,NR} ;


// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------

	class Movie
	{
	  public:
		  	
		// ------------------------------------------------------
		// ----- Constructors -----------------------------------
		// ------------------------------------------------------
			//Default constructor
			Movie();

			//Copy Constructors
			Movie(const string& title) ;

			Movie(const string& title, 
			      const string& director, 
			      Movie_Rating rating,
			      unsigned int year,
			      const string& path, const vector<string> &actors) ;
		  	
		// ------------------------------------------------------
		// ----- Destructor -------------------------------------
		// ------------------------------------------------------
		
			~Movie();  // To be implemented in a future assignment.
	  	
		// ------------------------------------------------------
		// ----- Inspectors -------------------------------------
		// ------------------------------------------------------
			//gets the movie title
			string getTitle() const ;

			//gets the movie director
			string getDirector() const ;
			
			//gets the movie rating
			Movie_Rating getRating() const ;
			
			//gets the release date of the movie
			unsigned int getYear() const ;
			
			//gets the imdb url of the movie
			string getURL() const ;

			//gets the actors of the movie
			vector<string> getActor() ;
		
	  	
		// ------------------------------------------------------
		// ----- Mutators ---------------------------------------
		// ------------------------------------------------------
			//sets the title of the movie
			void Movie::setTitle(const string& title);

			//sets the director of the movie
			void setDirector(const string& director) ;
			
			//sets the rating of the movie 
			void setRating(Movie_Rating rating)  ;
			
			//sets the year the movie was released
			void setYear(unsigned int year)  ;
			
			//sets the imdb url of the movie
			void setURL(const string& path)  ;

			//sets the actor(s) of the movie
			void setActor(vector<string> & actors) ;
			
			//converts the enum Movie_Rating to string type
			string convert_rating(Movie_Rating r);
			
	   //----------------------------------------------------------
	   //----- Facilitators ---------------------------------------
       //----------------------------------------------------------  	
			//prints the output
			string toString ();
			//---------------------------------------------------------------------
			//  The ostringstream type allows output to be directed to
			//  a string in the same style as usually done with a stream.
			//---------------------------------------------------------------------

			void output(ostream & out);
		

	// ----------------------------------------------------------
	// ----------------------------------------------------------

	  private:
		string       title_ ;
		string       director_ ;
		Movie_Rating rating_ ;
		unsigned int year_ ;
		string       url_ ;
		vector <string> actors_;
		
	};

#endif 
Last edited on
What are you trying to do with this?
actors_= actors_.push_back(actors);
If you're trying to use push_back, it should just be
actors_.push_back(actors);
you don't need to assign anything with the assignment operator.
with
actors_ = actors_.push_back(actors)
I'm trying to set it to actors_ so that when i use it on the toString() function it will print the actors that the user has input into the program
1
2
3
4
5
6
7
8
9
10
11
12
13
string Movie::toString () {

	ostringstream os;
	os << "Movie: " << title_ << endl 
	     << "Director: " << director_ << endl  
	     << "Year: " << year_ << endl
	     << "Rating: " << convert_rating(rating_) << endl
	     << "IMDB URL: " << url_ << endl
	     << "Actors: " << actors_ << endl;

	string s = os.str();
	return s;
}


I also tried actors_.push_back(actors); and unfortunately I still get the same error.

Last edited on
To set actors_ equal to actors:
actors_ = actors;
To add the names in actors to actors_:
1
2
for ( int i = 0; i < actors.size ( ); ++i )
    actors_.push_back ( actors[i] );
ok so I edited the code to be **credits to Yay295 :
1
2
3
4
5
6
void Movie::setActor(const vector<string> & actors) {
	for ( int i = 0; i < actors.size ( ); ++i ) {
		actors_.push_back ( actors[i] );
	}
	actors_ = actors;
}


but now I'm getting error in my toString() function in line 9 saying:
Error: No operator "<<" matches these operands
1
2
3
4
5
6
7
8
9
10
11
12
13
string Movie::toString () {

	ostringstream os;
	os << "Movie: " << title_ << endl 
	     << "Director: " << director_ << endl  
	     << "Year: " << year_ << endl
	     << "Rating: " << convert_rating(rating_) << endl
	     << "IMDB URL: " << url_ << endl
	     << "Actors: " << actors_ << endl; //error in << actors_

	string s = os.str();
	return s;
}

any idea whats causing this?
You can't '<<' a vector.
ah ok thanks solved it by using an iterator:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
string Movie::toString ()	{
	vector <string> :: iterator ActorIter;
	ostringstream os;
	os << "Movie: " << title_ << endl 
	     << "Director: " << director_ << endl  
	     << "Year: " << year_ << endl
	     << "Rating: " << convert_rating(rating_) << endl
	     << "IMDB URL: " << url_ << endl
             << "Actors: ";
	for (ActorIter = actors_.begin(); ActorIter != actors_.end();++ActorIter) 
         	cout << *ActorIter << endl;

			string s = os.str();
			return s;
		}
Topic archived. No new replies allowed.