vector push_back problem

Write your question here.
Hi, I can't understand why the following code compile. the error is:
Description Resource Path Location Type
passing 'const std::vector<Recipe>' as 'this' argument of 'void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = Recipe; _Alloc = std::allocator<Recipe>; std::vector<_Tp, _Alloc>::value_type = Recipe]' discards qualifiers [-fpermissive] User.cpp ‪/HW5‬ line 22 C/C++ Problem

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  class Recipe {
public:
	Recipe(string name1,string author1) : name(name1),author(author1){};
	Recipe(const Recipe& recipe);
private:
	string name;
	string author;

};
void User::postRecipe(const string& name) const{
	ostringstream ss;
	ss << Id;
    recipes.push_back(Recipe(name,ss.str())); //this line!!
}
class User {
public:
	User(unsigned int Id,string name,
    int yearsOfExpereince,string lastName);
	string getName() const;
	void postRecipe(const string& name) const;
private:
	unsigned int Id;
    vector<Recipe> recipes;
};

Id is an unsigned int
User is anoter class.
and the decleration of recipes is: vector<Recipe> recipes.
Thanks!!
Last edited on
closed account (2UD8vCM9)
show the user class please
Have you defined a copy constructor?
The member function postRecipe() is const hence you cannot modify a member variables like recipes
Thanks you! this was the problem.
Topic archived. No new replies allowed.