C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28695
Number of posts: 94715

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Noob C++: Constants and iterators - problems on usage. Posted by AndreiVictor on 14 Sept 2006 at 5:48 PM
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.
Report
Re: Noob C++: Constants and iterators - problems on usage. Posted by Donotalo on 15 Sept 2006 at 1:03 AM
: 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()

Report
Re: Noob C++: Constants and iterators - problems on usage. Posted by AndreiVictor on 17 Sept 2006 at 4:21 PM
: : 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!
Report
Re: Noob C++: Constants and iterators - problems on usage. Posted by Donotalo on 17 Sept 2006 at 8:30 PM
: : : 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()




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.