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?










share|improve this question




















  • 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















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?










share|improve this question




















  • 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













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?










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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














  • 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












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();





share|improve this answer























  • Thanks songyuanyao! That seems to have resolved the issue with the conversion error!
    – Rajat Mitra
    Nov 5 at 2:52











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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
































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();





share|improve this answer























  • Thanks songyuanyao! That seems to have resolved the issue with the conversion error!
    – Rajat Mitra
    Nov 5 at 2:52















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();





share|improve this answer























  • Thanks songyuanyao! That seems to have resolved the issue with the conversion error!
    – Rajat Mitra
    Nov 5 at 2:52













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();





share|improve this answer














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();






share|improve this answer














share|improve this answer



share|improve this answer








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


















  • 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


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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




















































































這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini