How do I declare an iterator for a map with the following template - std::map my_map?
up vote
0
down vote
favorite
I have the following class declaration -
template <typename T>
class Polynomial{
std::map<std::string, T> _polynomial_
}
In a member function I declared an iterator for this -
typename std::map<std::string, T>::iterator it= _polynomial_.begin();
The completed member function looks like this -
template <typename T>
void Polynomial<T>::print(std::ostream& out) const
{
typename std::map<std::string, T>::iterator it= _polynomial_.begin();
std::string term;
while(it != _polynomial_.end()){
term = it->second;
term += it->first;
if(it->first < (T)0){
out << "-" << term;
}
else{
out << "+" << term;
}
term = "";
it++;
}
}
In main, I call the function as follows -
Polynomial <double> p1;
p1.add_term("x0",9.862);
std::cout << p1;
However this does not seem to work and I get errors. GCC complains of a
conversion error -
Polynomial.hpp:32:47: error: conversion from u2018std::map, double, std::less >, std::allocator, double> > >::const_iterator {aka std::_Rb_tree_const_iterator, double> >}u2019 to non-scalar type u2018std::map, double, std::less >, std::allocator, double> > >::iterator {aka std::_Rb_tree_iterator, double> >}u2019 requested
typename std::map::iterator it= polynomial.begin();
Can someone tell me what is the correct declaration of the iterator?
c++ templates iterator stdmap
add a comment |
up vote
0
down vote
favorite
I have the following class declaration -
template <typename T>
class Polynomial{
std::map<std::string, T> _polynomial_
}
In a member function I declared an iterator for this -
typename std::map<std::string, T>::iterator it= _polynomial_.begin();
The completed member function looks like this -
template <typename T>
void Polynomial<T>::print(std::ostream& out) const
{
typename std::map<std::string, T>::iterator it= _polynomial_.begin();
std::string term;
while(it != _polynomial_.end()){
term = it->second;
term += it->first;
if(it->first < (T)0){
out << "-" << term;
}
else{
out << "+" << term;
}
term = "";
it++;
}
}
In main, I call the function as follows -
Polynomial <double> p1;
p1.add_term("x0",9.862);
std::cout << p1;
However this does not seem to work and I get errors. GCC complains of a
conversion error -
Polynomial.hpp:32:47: error: conversion from u2018std::map, double, std::less >, std::allocator, double> > >::const_iterator {aka std::_Rb_tree_const_iterator, double> >}u2019 to non-scalar type u2018std::map, double, std::less >, std::allocator, double> > >::iterator {aka std::_Rb_tree_iterator, double> >}u2019 requested
typename std::map::iterator it= polynomial.begin();
Can someone tell me what is the correct declaration of the iterator?
c++ templates iterator stdmap
3
Could you elaborate? It looks fine to me.
– chris
Nov 5 at 2:17
5
Read the errors. If the errors don't make sense to you, add the errors to your question. Alternatively provide a minimal complete program demonstrating the issue.
– paddy
Nov 5 at 2:26
I have added more details. Any help much appreciated...
– Rajat Mitra
Nov 5 at 2:40
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have the following class declaration -
template <typename T>
class Polynomial{
std::map<std::string, T> _polynomial_
}
In a member function I declared an iterator for this -
typename std::map<std::string, T>::iterator it= _polynomial_.begin();
The completed member function looks like this -
template <typename T>
void Polynomial<T>::print(std::ostream& out) const
{
typename std::map<std::string, T>::iterator it= _polynomial_.begin();
std::string term;
while(it != _polynomial_.end()){
term = it->second;
term += it->first;
if(it->first < (T)0){
out << "-" << term;
}
else{
out << "+" << term;
}
term = "";
it++;
}
}
In main, I call the function as follows -
Polynomial <double> p1;
p1.add_term("x0",9.862);
std::cout << p1;
However this does not seem to work and I get errors. GCC complains of a
conversion error -
Polynomial.hpp:32:47: error: conversion from u2018std::map, double, std::less >, std::allocator, double> > >::const_iterator {aka std::_Rb_tree_const_iterator, double> >}u2019 to non-scalar type u2018std::map, double, std::less >, std::allocator, double> > >::iterator {aka std::_Rb_tree_iterator, double> >}u2019 requested
typename std::map::iterator it= polynomial.begin();
Can someone tell me what is the correct declaration of the iterator?
c++ templates iterator stdmap
I have the following class declaration -
template <typename T>
class Polynomial{
std::map<std::string, T> _polynomial_
}
In a member function I declared an iterator for this -
typename std::map<std::string, T>::iterator it= _polynomial_.begin();
The completed member function looks like this -
template <typename T>
void Polynomial<T>::print(std::ostream& out) const
{
typename std::map<std::string, T>::iterator it= _polynomial_.begin();
std::string term;
while(it != _polynomial_.end()){
term = it->second;
term += it->first;
if(it->first < (T)0){
out << "-" << term;
}
else{
out << "+" << term;
}
term = "";
it++;
}
}
In main, I call the function as follows -
Polynomial <double> p1;
p1.add_term("x0",9.862);
std::cout << p1;
However this does not seem to work and I get errors. GCC complains of a
conversion error -
Polynomial.hpp:32:47: error: conversion from u2018std::map, double, std::less >, std::allocator, double> > >::const_iterator {aka std::_Rb_tree_const_iterator, double> >}u2019 to non-scalar type u2018std::map, double, std::less >, std::allocator, double> > >::iterator {aka std::_Rb_tree_iterator, double> >}u2019 requested
typename std::map::iterator it= polynomial.begin();
Can someone tell me what is the correct declaration of the iterator?
c++ templates iterator stdmap
c++ templates iterator stdmap
edited Nov 5 at 2:39
asked Nov 5 at 2:16
Rajat Mitra
307
307
3
Could you elaborate? It looks fine to me.
– chris
Nov 5 at 2:17
5
Read the errors. If the errors don't make sense to you, add the errors to your question. Alternatively provide a minimal complete program demonstrating the issue.
– paddy
Nov 5 at 2:26
I have added more details. Any help much appreciated...
– Rajat Mitra
Nov 5 at 2:40
add a comment |
3
Could you elaborate? It looks fine to me.
– chris
Nov 5 at 2:17
5
Read the errors. If the errors don't make sense to you, add the errors to your question. Alternatively provide a minimal complete program demonstrating the issue.
– paddy
Nov 5 at 2:26
I have added more details. Any help much appreciated...
– Rajat Mitra
Nov 5 at 2:40
3
3
Could you elaborate? It looks fine to me.
– chris
Nov 5 at 2:17
Could you elaborate? It looks fine to me.
– chris
Nov 5 at 2:17
5
5
Read the errors. If the errors don't make sense to you, add the errors to your question. Alternatively provide a minimal complete program demonstrating the issue.
– paddy
Nov 5 at 2:26
Read the errors. If the errors don't make sense to you, add the errors to your question. Alternatively provide a minimal complete program demonstrating the issue.
– paddy
Nov 5 at 2:26
I have added more details. Any help much appreciated...
– Rajat Mitra
Nov 5 at 2:40
I have added more details. Any help much appreciated...
– Rajat Mitra
Nov 5 at 2:40
add a comment |
1 Answer
1
active
oldest
votes
up vote
3
down vote
accepted
Polynomial<T>::print
is a const
member function, inside which the data member _polynomial_
becomes const
too, that means what _polynomial_.begin()
returns is a const_iterator
, which can't be converted to iterator
implicitly. (Note that std::map::begin
is overloaded with const
version and non-const
version, the former returns const_iterator
and the latter returns iterator
.)
Change the code to
typename std::map<std::string, T>::const_iterator it = _polynomial_.begin();
// ^^^^^^
or use auto
instead, it would deduce the correct type for you.
auto it = _polynomial_.begin();
Thanks songyuanyao! That seems to have resolved the issue with the conversion error!
– Rajat Mitra
Nov 5 at 2:52
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
Polynomial<T>::print
is a const
member function, inside which the data member _polynomial_
becomes const
too, that means what _polynomial_.begin()
returns is a const_iterator
, which can't be converted to iterator
implicitly. (Note that std::map::begin
is overloaded with const
version and non-const
version, the former returns const_iterator
and the latter returns iterator
.)
Change the code to
typename std::map<std::string, T>::const_iterator it = _polynomial_.begin();
// ^^^^^^
or use auto
instead, it would deduce the correct type for you.
auto it = _polynomial_.begin();
Thanks songyuanyao! That seems to have resolved the issue with the conversion error!
– Rajat Mitra
Nov 5 at 2:52
add a comment |
up vote
3
down vote
accepted
Polynomial<T>::print
is a const
member function, inside which the data member _polynomial_
becomes const
too, that means what _polynomial_.begin()
returns is a const_iterator
, which can't be converted to iterator
implicitly. (Note that std::map::begin
is overloaded with const
version and non-const
version, the former returns const_iterator
and the latter returns iterator
.)
Change the code to
typename std::map<std::string, T>::const_iterator it = _polynomial_.begin();
// ^^^^^^
or use auto
instead, it would deduce the correct type for you.
auto it = _polynomial_.begin();
Thanks songyuanyao! That seems to have resolved the issue with the conversion error!
– Rajat Mitra
Nov 5 at 2:52
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
Polynomial<T>::print
is a const
member function, inside which the data member _polynomial_
becomes const
too, that means what _polynomial_.begin()
returns is a const_iterator
, which can't be converted to iterator
implicitly. (Note that std::map::begin
is overloaded with const
version and non-const
version, the former returns const_iterator
and the latter returns iterator
.)
Change the code to
typename std::map<std::string, T>::const_iterator it = _polynomial_.begin();
// ^^^^^^
or use auto
instead, it would deduce the correct type for you.
auto it = _polynomial_.begin();
Polynomial<T>::print
is a const
member function, inside which the data member _polynomial_
becomes const
too, that means what _polynomial_.begin()
returns is a const_iterator
, which can't be converted to iterator
implicitly. (Note that std::map::begin
is overloaded with const
version and non-const
version, the former returns const_iterator
and the latter returns iterator
.)
Change the code to
typename std::map<std::string, T>::const_iterator it = _polynomial_.begin();
// ^^^^^^
or use auto
instead, it would deduce the correct type for you.
auto it = _polynomial_.begin();
edited Nov 5 at 2:50
answered Nov 5 at 2:43
songyuanyao
87.6k10169231
87.6k10169231
Thanks songyuanyao! That seems to have resolved the issue with the conversion error!
– Rajat Mitra
Nov 5 at 2:52
add a comment |
Thanks songyuanyao! That seems to have resolved the issue with the conversion error!
– Rajat Mitra
Nov 5 at 2:52
Thanks songyuanyao! That seems to have resolved the issue with the conversion error!
– Rajat Mitra
Nov 5 at 2:52
Thanks songyuanyao! That seems to have resolved the issue with the conversion error!
– Rajat Mitra
Nov 5 at 2:52
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53147471%2fhow-do-i-declare-an-iterator-for-a-map-with-the-following-template-stdmapst%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
3
Could you elaborate? It looks fine to me.
– chris
Nov 5 at 2:17
5
Read the errors. If the errors don't make sense to you, add the errors to your question. Alternatively provide a minimal complete program demonstrating the issue.
– paddy
Nov 5 at 2:26
I have added more details. Any help much appreciated...
– Rajat Mitra
Nov 5 at 2:40