Question about BOOST_FOREACH

I have declared this member in a class:

std::vector<std::pair<std::string, std::string> > m_foo;

but when I do this ...
1
2
3
BOOST_FOREACH(std::pair<std::string, std::string>& entry, m_foo){
	// do stuff
}


... the compiler complaints with a series of confusing messages: (I translated the first line, the rest seems to be generic syntax errors resulting from the first line)

1>.\fb_master.cpp(159) : warning C4002: too many actual parameters for macro 'BOOST_FOREACH'
1>.\fb_master.cpp(159) : error C2143: Syntaxfehler: Es fehlt ')' vor '>'
1>.\fb_master.cpp(159) : error C2059: Syntaxfehler: '>'
1>.\fb_master.cpp(159) : error C2059: Syntaxfehler: ')'
1>.\fb_master.cpp(159) : error C2143: Syntaxfehler: Es fehlt ';' vor '{'
1>.\fb_master.cpp(159) : error C2181: Ungültiges 'else' ohne zugehöriges 'if'
1>.\fb_master.cpp(159) : error C2065: 'entry': nichtdeklarierter Bezeichner
1>.\fb_master.cpp(159) : error C2275: 'std::string': Ungültige Verwendung dieses Typs als Ausdruck
1>.\fb_master.cpp(159) : error C2065: 'entry': nichtdeklarierter Bezeichner
1>.\fb_master.cpp(159) : error C2275: 'std::string': Ungültige Verwendung dieses Typs als Ausdruck
1>.\fb_master.cpp(159) : error C2065: 'entry': nichtdeklarierter Bezeichner
1>.\fb_master.cpp(159) : error C2275: 'std::string': Ungültige Verwendung dieses Typs als Ausdruck
1>.\fb_master.cpp(159) : error C2065: 'entry': nichtdeklarierter Bezeichner
1>.\fb_master.cpp(159) : error C2275: 'std::string': Ungültige Verwendung dieses Typs als Ausdruck
1>.\fb_master.cpp(159) : error C2143: Syntaxfehler: Es fehlt ')' vor '>'
1>.\fb_master.cpp(159) : error C2059: Syntaxfehler: '>'
1>.\fb_master.cpp(159) : error C2059: Syntaxfehler: ')'
1>.\fb_master.cpp(159) : error C2059: Syntaxfehler: 'else'
1>.\fb_master.cpp(159) : error C2143: Syntaxfehler: Es fehlt ')' vor '>'
1>.\fb_master.cpp(159) : error C4430: Fehlender Typspezifizierer - int wird angenommen. Hinweis: "default-int" wird von C++ nicht unterstützt.
1>.\fb_master.cpp(159) : error C2244: 'encode_type': Keine Übereinstimmung für Funktionsdefinition mit vorhandener Deklaration gefunden


But this works:

1
2
3
4
5
typedef std::pair<std::string, std::string> foo_t;

BOOST_FOREACH(foo_t& entry, m_foo){
	// do stuff
}


Unfortunatly I have this situation a lot with different combinations of types and I don't want to typedef all of them.
Or at least I wouldn't like it because it feels like a cheap workaround.

And no, I can't use auto. I have to use a C++98 compiler.

So, the question is: what am I doing wrong?
Last edited on
Macros don't know what C or C++ is. From their perspective, that comma after the first std::string separates two macro parameters, not two template parameters. Typedef is the way to go.
Last edited on
Damn. The comma thing was my first guess, too.
All right, typedef it is. Thanks.
Just for completion, for anyone who isn't stuck with an old compiler, we have ranged for loops and auto now.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <vector>
#include <string>

int main()
{
  std::vector<std::pair<std::string, std::string> > m_foo;

  m_foo.emplace_back(std::make_pair("Beans", "cats"));
  m_foo.emplace_back(std::make_pair("Kittens", "peanuts"));

  for (const auto& item : m_foo)
    {
      std::cout << item.first << " " << item.second << std::endl;
    }

}

Last edited on
@Moschops: As I already mentioned, I have to use a C++98 compiler.
I'm well aware of state-of-the-art C++.
@plexus

As I already mentioned, "for anyone who isn't stuck with an old compiler".
@Moschops:

Come on, that's not nice.
You edit your post and then write a reply that makes me look like an idiot.

The final edit to my post was eight minutes before you replied. If you were still working on a previous version, then I apologise; it seemed safe to assume that it hadn't taken you eight minutes to write that, but I can imagine circumstances in which you were working with an old iteration.
Last edited on
Ok, I see - my bad. I guess I got sidetracked while replying.
Moschops wrote:
it seemed safe to assume that it hadn't taken you eight minutes to write that, but I can imagine circumstances in which you were working with an old iteration.

I frequently find myself starting a reply, then having to leave it to do something more urgent, coming back to it later. 8 minutes is nothing; I don't think it's fair to assume that at all.
Topic archived. No new replies allowed.