By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,708 Members | 1,553 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,708 IT Pros & Developers. It's quick & easy.

error C2662 cannot convert this pointer..

P: 9
i am getting this error

error C2662: 'std::vector<_Ty>::push_back' : cannot convert 'this' pointer from 'const std::vector<_Ty>' to 'std::vector<_Ty> &'
with
[
_Ty=std::string
]
and
[
_Ty=std::string
]
and
[
_Ty=std::string
]


this is my code inside the class customer

void customer::add_movie(string move) const
{
int flag1=0;
for (unsigned int i=0; i<movies.size(); i++)
{
if (move==movies[i])
flag1=1;
}
if (flag1=0)
movies.push_back(move); // error is here
}

movies is a private member variable: vector<string> movies.

ive tried doing different things, but i cant fix it, can anyone help please?
Apr 6 '07 #1
Share this Question
Share on Google+
3 Replies


P: 9
bumpssssssssssss
Apr 6 '07 #2

Banfa
Expert Mod 5K+
P: 8,916
bumpssssssssssss
Only waiting an hour before bumping is really not waiting long enough. The forum experts are not all in the same time zone as you. You should leave a post at least 24 hours before bumping.
Apr 6 '07 #3

Banfa
Expert Mod 5K+
P: 8,916
error C2662: 'std::vector<_Ty>::push_back' : cannot convert 'this' pointer from 'const std::vector<_Ty>' to 'std::vector<_Ty> &'
with
[
_Ty=std::string
]
and
[
_Ty=std::string
]
and
[
_Ty=std::string
]
Read the error, the is a type mismatch between 'const std::vector<_Ty>' to 'std::vector<_Ty> &' where_ty=std:string (or string). Substituting there is a type mismatch between 'const std::vector<string>' to 'std::vector<string> &', you can not convert between a const object an a non-const reference to an object.

Now look at your function

Expand|Select|Wrap|Line Numbers
  1. void customer::add_movie(string move) const
  2. {
  3.     int flag1=0;
  4.     for (unsigned int i=0; i<movies.size(); i++)
  5.     {
  6.         if (move==movies[i])
  7.             flag1=1;
  8.     }
  9.     if (flag1=0)
  10.         movies.push_back(move); // error is here
  11. }
movies is a private member variable: vector<string> movies.
See any const qualifiers? Yes on the function, that const is telling the compiler that this function does not alter any member variables and that all member variables may be treated as const in this function. However you are trying to change movies.

Remove the const qualifier from the function.
Apr 6 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.