Why is there a syntax error in my map iterator?Where and why do I have to put the “template” and “typename” keywords?How do I iterate over the words of a string?Why can templates only be implemented in the header file?Why is “using namespace std;” considered bad practice?Why do we need virtual functions in C++?Why are elementwise additions much faster in separate loops than in a combined loop?Why does changing 0.1f to 0 slow down performance by 10x?Why is reading lines from stdin much slower in C++ than Python?Why is processing a sorted array faster than processing an unsorted array?Why should I use a pointer rather than the object itself?Use std container iterator in template class

When to use и or а as “and”?

How do I type a hyphen in iOS 12?

Dependent voltage/current sources

What class is best to play when a level behind the rest of the party?

Why is it bad to use your whole foot in rock climbing

Is all-caps blackletter no longer taboo?

Print "N NE E SE S SW W NW"

Does a single fopen introduce TOCTOU vulnerability?

How can powerful telekinesis avoid violating Newton's 3rd Law?

Nth term of Van Eck Sequence

How to make a composition of functions prettier?

What do I need to do, tax-wise, for a sudden windfall?

How to use the word seem

What is the theme of analysis?

How many sets of dice do I need for D&D?

Quasar Redshifts

What did the 8086 (and 8088) do upon encountering an illegal instruction?

Convert GE Load Center to main breaker

Finding diameter of a circle using two chords and angle between them

How much web presence should I have?

Is Lambda Calculus purely syntactic?

What's the best way to quit a job mostly because of money?

Why did the World Bank set the global poverty line at $1.90?

Linked novellas where humans are engineered to adapt to a variety of environments



Why is there a syntax error in my map iterator?


Where and why do I have to put the “template” and “typename” keywords?How do I iterate over the words of a string?Why can templates only be implemented in the header file?Why is “using namespace std;” considered bad practice?Why do we need virtual functions in C++?Why are elementwise additions much faster in separate loops than in a combined loop?Why does changing 0.1f to 0 slow down performance by 10x?Why is reading lines from stdin much slower in C++ than Python?Why is processing a sorted array faster than processing an unsorted array?Why should I use a pointer rather than the object itself?Use std container iterator in template class






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








-1















I wrote a template function for flipping my std::map keys and values.



#include <map>
#include <iterator>

template <typename A, typename B>
std::map<B, A> flip_map(std::map<A, B> &src)

std::map<B, A> dst;
for (std::map<A, B>::iterator it = src.begin(); it != src.end(); ++it)

dst.insert(std::pair<B, A>(it->second, it->first));

return dst;



VS gives me a syntax error:




unexpected token 'identifier', expected ';'




I don't know what I'm doing wrong.










share|improve this question



















  • 1





    Remember, auto is your friend. Let it do the work for you. for (std::map<A, B>::iterator it = src.begin(); it != src.end(); ++it) -> for (auto it = src.begin(); it != src.end(); ++it) or better yet for (auto e : dst)

    – NathanOliver
    Mar 24 at 23:21











  • @NathanOliver Or, better yet, for (auto e : src)

    – Lightness Races in Orbit
    Mar 24 at 23:23











  • The best one is: for (auto&[key, value] : src) ...

    – Dmitry
    Mar 24 at 23:24

















-1















I wrote a template function for flipping my std::map keys and values.



#include <map>
#include <iterator>

template <typename A, typename B>
std::map<B, A> flip_map(std::map<A, B> &src)

std::map<B, A> dst;
for (std::map<A, B>::iterator it = src.begin(); it != src.end(); ++it)

dst.insert(std::pair<B, A>(it->second, it->first));

return dst;



VS gives me a syntax error:




unexpected token 'identifier', expected ';'




I don't know what I'm doing wrong.










share|improve this question



















  • 1





    Remember, auto is your friend. Let it do the work for you. for (std::map<A, B>::iterator it = src.begin(); it != src.end(); ++it) -> for (auto it = src.begin(); it != src.end(); ++it) or better yet for (auto e : dst)

    – NathanOliver
    Mar 24 at 23:21











  • @NathanOliver Or, better yet, for (auto e : src)

    – Lightness Races in Orbit
    Mar 24 at 23:23











  • The best one is: for (auto&[key, value] : src) ...

    – Dmitry
    Mar 24 at 23:24













-1












-1








-1








I wrote a template function for flipping my std::map keys and values.



#include <map>
#include <iterator>

template <typename A, typename B>
std::map<B, A> flip_map(std::map<A, B> &src)

std::map<B, A> dst;
for (std::map<A, B>::iterator it = src.begin(); it != src.end(); ++it)

dst.insert(std::pair<B, A>(it->second, it->first));

return dst;



VS gives me a syntax error:




unexpected token 'identifier', expected ';'




I don't know what I'm doing wrong.










share|improve this question
















I wrote a template function for flipping my std::map keys and values.



#include <map>
#include <iterator>

template <typename A, typename B>
std::map<B, A> flip_map(std::map<A, B> &src)

std::map<B, A> dst;
for (std::map<A, B>::iterator it = src.begin(); it != src.end(); ++it)

dst.insert(std::pair<B, A>(it->second, it->first));

return dst;



VS gives me a syntax error:




unexpected token 'identifier', expected ';'




I don't know what I'm doing wrong.







c++






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 25 at 0:38









Remy Lebeau

351k19277474




351k19277474










asked Mar 24 at 23:14









MossiMossi

81




81







  • 1





    Remember, auto is your friend. Let it do the work for you. for (std::map<A, B>::iterator it = src.begin(); it != src.end(); ++it) -> for (auto it = src.begin(); it != src.end(); ++it) or better yet for (auto e : dst)

    – NathanOliver
    Mar 24 at 23:21











  • @NathanOliver Or, better yet, for (auto e : src)

    – Lightness Races in Orbit
    Mar 24 at 23:23











  • The best one is: for (auto&[key, value] : src) ...

    – Dmitry
    Mar 24 at 23:24












  • 1





    Remember, auto is your friend. Let it do the work for you. for (std::map<A, B>::iterator it = src.begin(); it != src.end(); ++it) -> for (auto it = src.begin(); it != src.end(); ++it) or better yet for (auto e : dst)

    – NathanOliver
    Mar 24 at 23:21











  • @NathanOliver Or, better yet, for (auto e : src)

    – Lightness Races in Orbit
    Mar 24 at 23:23











  • The best one is: for (auto&[key, value] : src) ...

    – Dmitry
    Mar 24 at 23:24







1




1





Remember, auto is your friend. Let it do the work for you. for (std::map<A, B>::iterator it = src.begin(); it != src.end(); ++it) -> for (auto it = src.begin(); it != src.end(); ++it) or better yet for (auto e : dst)

– NathanOliver
Mar 24 at 23:21





Remember, auto is your friend. Let it do the work for you. for (std::map<A, B>::iterator it = src.begin(); it != src.end(); ++it) -> for (auto it = src.begin(); it != src.end(); ++it) or better yet for (auto e : dst)

– NathanOliver
Mar 24 at 23:21













@NathanOliver Or, better yet, for (auto e : src)

– Lightness Races in Orbit
Mar 24 at 23:23





@NathanOliver Or, better yet, for (auto e : src)

– Lightness Races in Orbit
Mar 24 at 23:23













The best one is: for (auto&[key, value] : src) ...

– Dmitry
Mar 24 at 23:24





The best one is: for (auto&[key, value] : src) ...

– Dmitry
Mar 24 at 23:24












2 Answers
2






active

oldest

votes


















5














GCC tells you more clearly what's wrong: you need typename there, for arcane C++ reasons.



Like this:



for (typename std::map<A, B>::iterator it = src.begin(); it != src.end(); ++it)
// ^^^^^^^^


Further reading:



  • Where and why do I have to put the "template" and "typename" keywords?


I'd instead write it like this:



#include <map>

template <typename A, typename B>
std::map<B, A> flip_map(const std::map<A, B>& src)

std::map<B, A> dst;

for (const auto& p : src)
dst.insert(std::make_pair(p.second, p.first));

return dst;



In fact, as it happens, I did; two weeks ago. :)



(You could also consider some .emplace and std::move etc, depending on what A and B are likely to be, though since you cannot move from a map key this is only ever going to be "sort of useful".)






share|improve this answer























  • Thank you man. nice answer.

    – Mossi
    Mar 25 at 14:56


















2














The reason is that std::map<A,B>::iterator is a dependent name (in rough terms, it is in a templated function, and depends on A and B which are parameters of that template). So it needs to be preceded with the typename keyword.



template <typename A, typename B>
std::map<B, A> flip_map(std::map<A, B> &src)

std::map<B, A> dst;
for (typename std::map<A, B>::iterator it = src.begin(); it != src.end(); ++it)

dst.insert(std::pair<B, A>(it->second, it->first));

return dst;



Additionally you would probably be better specifying src as const, and using const_iterator rather than iterator, viz



template <typename A, typename B>
std::map<B, A> flip_map(const std::map<A, B> &src)

std::map<B, A> dst;
for (typename std::map<A, B>::const_iterator it = src.begin(); it != src.end(); ++it)

dst.insert(std::pair<B, A>(it->second, it->first));

return dst;



or (C++11 and later), let the compiler do the type deduction for you by using auto and std::make_pair. This avoids need for the programmer to worry about dependent names.



template <typename A, typename B>
std::map<B, A> flip_map(const std::map<A, B> &src)

std::map<B, A> dst;
for (const auto &e : src)

dst.insert(std::make_pair(e.second, e.first));

return dst;






share|improve this answer























  • Thank you, it's a complete answer.

    – Mossi
    Mar 25 at 14:55











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',
autoActivateHeartbeat: false,
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%2f55329483%2fwhy-is-there-a-syntax-error-in-my-map-iterator%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









5














GCC tells you more clearly what's wrong: you need typename there, for arcane C++ reasons.



Like this:



for (typename std::map<A, B>::iterator it = src.begin(); it != src.end(); ++it)
// ^^^^^^^^


Further reading:



  • Where and why do I have to put the "template" and "typename" keywords?


I'd instead write it like this:



#include <map>

template <typename A, typename B>
std::map<B, A> flip_map(const std::map<A, B>& src)

std::map<B, A> dst;

for (const auto& p : src)
dst.insert(std::make_pair(p.second, p.first));

return dst;



In fact, as it happens, I did; two weeks ago. :)



(You could also consider some .emplace and std::move etc, depending on what A and B are likely to be, though since you cannot move from a map key this is only ever going to be "sort of useful".)






share|improve this answer























  • Thank you man. nice answer.

    – Mossi
    Mar 25 at 14:56















5














GCC tells you more clearly what's wrong: you need typename there, for arcane C++ reasons.



Like this:



for (typename std::map<A, B>::iterator it = src.begin(); it != src.end(); ++it)
// ^^^^^^^^


Further reading:



  • Where and why do I have to put the "template" and "typename" keywords?


I'd instead write it like this:



#include <map>

template <typename A, typename B>
std::map<B, A> flip_map(const std::map<A, B>& src)

std::map<B, A> dst;

for (const auto& p : src)
dst.insert(std::make_pair(p.second, p.first));

return dst;



In fact, as it happens, I did; two weeks ago. :)



(You could also consider some .emplace and std::move etc, depending on what A and B are likely to be, though since you cannot move from a map key this is only ever going to be "sort of useful".)






share|improve this answer























  • Thank you man. nice answer.

    – Mossi
    Mar 25 at 14:56













5












5








5







GCC tells you more clearly what's wrong: you need typename there, for arcane C++ reasons.



Like this:



for (typename std::map<A, B>::iterator it = src.begin(); it != src.end(); ++it)
// ^^^^^^^^


Further reading:



  • Where and why do I have to put the "template" and "typename" keywords?


I'd instead write it like this:



#include <map>

template <typename A, typename B>
std::map<B, A> flip_map(const std::map<A, B>& src)

std::map<B, A> dst;

for (const auto& p : src)
dst.insert(std::make_pair(p.second, p.first));

return dst;



In fact, as it happens, I did; two weeks ago. :)



(You could also consider some .emplace and std::move etc, depending on what A and B are likely to be, though since you cannot move from a map key this is only ever going to be "sort of useful".)






share|improve this answer













GCC tells you more clearly what's wrong: you need typename there, for arcane C++ reasons.



Like this:



for (typename std::map<A, B>::iterator it = src.begin(); it != src.end(); ++it)
// ^^^^^^^^


Further reading:



  • Where and why do I have to put the "template" and "typename" keywords?


I'd instead write it like this:



#include <map>

template <typename A, typename B>
std::map<B, A> flip_map(const std::map<A, B>& src)

std::map<B, A> dst;

for (const auto& p : src)
dst.insert(std::make_pair(p.second, p.first));

return dst;



In fact, as it happens, I did; two weeks ago. :)



(You could also consider some .emplace and std::move etc, depending on what A and B are likely to be, though since you cannot move from a map key this is only ever going to be "sort of useful".)







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 24 at 23:17









Lightness Races in OrbitLightness Races in Orbit

303k56491845




303k56491845












  • Thank you man. nice answer.

    – Mossi
    Mar 25 at 14:56

















  • Thank you man. nice answer.

    – Mossi
    Mar 25 at 14:56
















Thank you man. nice answer.

– Mossi
Mar 25 at 14:56





Thank you man. nice answer.

– Mossi
Mar 25 at 14:56













2














The reason is that std::map<A,B>::iterator is a dependent name (in rough terms, it is in a templated function, and depends on A and B which are parameters of that template). So it needs to be preceded with the typename keyword.



template <typename A, typename B>
std::map<B, A> flip_map(std::map<A, B> &src)

std::map<B, A> dst;
for (typename std::map<A, B>::iterator it = src.begin(); it != src.end(); ++it)

dst.insert(std::pair<B, A>(it->second, it->first));

return dst;



Additionally you would probably be better specifying src as const, and using const_iterator rather than iterator, viz



template <typename A, typename B>
std::map<B, A> flip_map(const std::map<A, B> &src)

std::map<B, A> dst;
for (typename std::map<A, B>::const_iterator it = src.begin(); it != src.end(); ++it)

dst.insert(std::pair<B, A>(it->second, it->first));

return dst;



or (C++11 and later), let the compiler do the type deduction for you by using auto and std::make_pair. This avoids need for the programmer to worry about dependent names.



template <typename A, typename B>
std::map<B, A> flip_map(const std::map<A, B> &src)

std::map<B, A> dst;
for (const auto &e : src)

dst.insert(std::make_pair(e.second, e.first));

return dst;






share|improve this answer























  • Thank you, it's a complete answer.

    – Mossi
    Mar 25 at 14:55















2














The reason is that std::map<A,B>::iterator is a dependent name (in rough terms, it is in a templated function, and depends on A and B which are parameters of that template). So it needs to be preceded with the typename keyword.



template <typename A, typename B>
std::map<B, A> flip_map(std::map<A, B> &src)

std::map<B, A> dst;
for (typename std::map<A, B>::iterator it = src.begin(); it != src.end(); ++it)

dst.insert(std::pair<B, A>(it->second, it->first));

return dst;



Additionally you would probably be better specifying src as const, and using const_iterator rather than iterator, viz



template <typename A, typename B>
std::map<B, A> flip_map(const std::map<A, B> &src)

std::map<B, A> dst;
for (typename std::map<A, B>::const_iterator it = src.begin(); it != src.end(); ++it)

dst.insert(std::pair<B, A>(it->second, it->first));

return dst;



or (C++11 and later), let the compiler do the type deduction for you by using auto and std::make_pair. This avoids need for the programmer to worry about dependent names.



template <typename A, typename B>
std::map<B, A> flip_map(const std::map<A, B> &src)

std::map<B, A> dst;
for (const auto &e : src)

dst.insert(std::make_pair(e.second, e.first));

return dst;






share|improve this answer























  • Thank you, it's a complete answer.

    – Mossi
    Mar 25 at 14:55













2












2








2







The reason is that std::map<A,B>::iterator is a dependent name (in rough terms, it is in a templated function, and depends on A and B which are parameters of that template). So it needs to be preceded with the typename keyword.



template <typename A, typename B>
std::map<B, A> flip_map(std::map<A, B> &src)

std::map<B, A> dst;
for (typename std::map<A, B>::iterator it = src.begin(); it != src.end(); ++it)

dst.insert(std::pair<B, A>(it->second, it->first));

return dst;



Additionally you would probably be better specifying src as const, and using const_iterator rather than iterator, viz



template <typename A, typename B>
std::map<B, A> flip_map(const std::map<A, B> &src)

std::map<B, A> dst;
for (typename std::map<A, B>::const_iterator it = src.begin(); it != src.end(); ++it)

dst.insert(std::pair<B, A>(it->second, it->first));

return dst;



or (C++11 and later), let the compiler do the type deduction for you by using auto and std::make_pair. This avoids need for the programmer to worry about dependent names.



template <typename A, typename B>
std::map<B, A> flip_map(const std::map<A, B> &src)

std::map<B, A> dst;
for (const auto &e : src)

dst.insert(std::make_pair(e.second, e.first));

return dst;






share|improve this answer













The reason is that std::map<A,B>::iterator is a dependent name (in rough terms, it is in a templated function, and depends on A and B which are parameters of that template). So it needs to be preceded with the typename keyword.



template <typename A, typename B>
std::map<B, A> flip_map(std::map<A, B> &src)

std::map<B, A> dst;
for (typename std::map<A, B>::iterator it = src.begin(); it != src.end(); ++it)

dst.insert(std::pair<B, A>(it->second, it->first));

return dst;



Additionally you would probably be better specifying src as const, and using const_iterator rather than iterator, viz



template <typename A, typename B>
std::map<B, A> flip_map(const std::map<A, B> &src)

std::map<B, A> dst;
for (typename std::map<A, B>::const_iterator it = src.begin(); it != src.end(); ++it)

dst.insert(std::pair<B, A>(it->second, it->first));

return dst;



or (C++11 and later), let the compiler do the type deduction for you by using auto and std::make_pair. This avoids need for the programmer to worry about dependent names.



template <typename A, typename B>
std::map<B, A> flip_map(const std::map<A, B> &src)

std::map<B, A> dst;
for (const auto &e : src)

dst.insert(std::make_pair(e.second, e.first));

return dst;







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 25 at 0:02









PeterPeter

28.5k32257




28.5k32257












  • Thank you, it's a complete answer.

    – Mossi
    Mar 25 at 14:55

















  • Thank you, it's a complete answer.

    – Mossi
    Mar 25 at 14:55
















Thank you, it's a complete answer.

– Mossi
Mar 25 at 14:55





Thank you, it's a complete answer.

– Mossi
Mar 25 at 14:55

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid …


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55329483%2fwhy-is-there-a-syntax-error-in-my-map-iterator%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to preserve electronics (computers, tablets and phones) for hundreds of years Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) The network's official Twitter account is up and running again. What content…How might modern humans leave a message for 50,000 years?How would an aquatic race develop computers?How do we make computers care for human life?Keeping supplies for 1000 yearsWhat will cell phones become in 100 years?What body armor protects against a laser?Could a society attain 1800s technology if limited to using copper and bronze?Electricity power (magic) and electronicsScavenging metal resources in post-apocalypse after 100 yearsWhy would precursors create devices that can survive and still work after hundreds of years?

155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해

해남서초등학교 학교 연혁 학교 동문 외부 링크 둘러보기 메뉴북위 34° 34′ 21″ 동경 126° 35′ 50″ / 북위 34.5726385° 동경 126.5972670°  / 34.5726385; 126.5972670해남서초등학교eh