: : : Hello,
: : :
: : : I'm fairly new to C++ and I am wondering why my code does not compile:
: : :
: : :
: : : void PGE::I_Console::CommandDefault(const vector<string> & p_vArgs) {
: : : for(vector<string>::iterator iter = p_vArgs.begin(); iter != p_vArgs.end(); iter++)
: : : this->Echo(p_vArgs[iter].c_str());
: : : }
: : :
: : :
: : : and gives the error:
: : :
error C2440: 'initializing' : cannot convert from 'std::_Vector_const_iterator<_Ty,_Alloc>' to 'std::_Vector_iterator<_Ty,_Alloc>'
: : :
: : : I know its a casting error, but I also tried:
: : :
: : :
: : : for(const vector<string>::iterator iter = p_vArgs.begin(); iter != p_vArgs.end(); iter++)
: : :
: : :
: : : and still it produces the same error.
: : :
: : : I am new to C++ and the function definition of CommandDefault is already provided and I just need to fill in the implementation. So, I am puzzled also what "const vector<string>" mean? Is it a vector of constant strings or is it a constant vector of strings? Forgive me, I am the type of person who doesn't use const keyword (I just disciplined myself that I dont modify variables that I want to be treated as constants) hence, the usage and concept of const keyword is new to me. And, so are iterators.
: : :
: :
: : vector<string> is a vector of string and const modifier implies that u cannot change anything of this vector. iterators are like pointers. they operate on STL classes.
: :
: : i think the following will solve ur compile error:
: : void PGE::I_Console::CommandDefault(const vector<string> & p_vArgs) {
: : for(vector<string>::const_iterator iter = p_vArgs.begin(); iter != p_vArgs.end(); iter++)
: : this->Echo(p_vArgs[iter].c_str());
: : }
: :
: : ie, the iterator has to be constant.
: :
: :
~Donotalo()
: :
: :
:
: Hi! Sorry for the late reply as I just waited for the weekend to pass to get back to this. Thank you for the clarifications and thanks for the fix! It worked well. Since you also mentioned iterators are just like pointers, my usage on the last and innermost line is incorrect too. I fixed it. Again, thank you for your help!
:
Oh! shame to me, i didn't notice that! 
~Donotalo()