Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

Just playing around with the useful BOOST_FOREACH loop. I have run into a syntax problem

C++
std::vector < std::pair < int, int > > aa;
BOOST_FOREACH(std::pair<int,int>   ptr, aa)
{
              std::cout  << "hello";
}


with the compile errors

C++
ngonl/src/bom/ProActiveOnloadList.cpp:134:56: macro "BOOST_FOREACH" passed 3 arguments, but takes just 2
ngonl/src/bom/ProActiveOnloadList.cpp:134:56: macro "BOOST_FOREACH" passed 3 arguments, but takes just 2
ngonl/src/bom/ProActiveOnloadList.cpp: In function `void <unnamed>::rejectRebateStaff(std::vector<std::pair xmlns:std="#unknown"><com::proactiveonloadpax::segment,>, std::allocator<std::pair><com::proactiveonloadpax::segment,> > >, const COM::PublishedFlightLegDate&)':
ngonl/src/bom/ProActiveOnloadList.cpp: In function `void <unnamed>::rejectRebateStaff(std::vector<std::pair><com::proactiveonloadpax::segment,>, std::allocator<std::pair><com::proactiveonloadpax::segment,> > >, const COM::PublishedFlightLegDate&)':
ngonl/src/bom/ProActiveOnloadList.cpp:135: error: `BOOST_FOREACH' undeclared (first use this function)
ngonl/src/bom/ProActiveOnloadList.cpp:135: error: `BOOST_FOREACH' undeclared (first use this function)
ngonl/src/bom/ProActiveOnloadList.cpp:135: error: (Each undeclared identifier is reported only once for each function it appears in.)
ngonl/src/bom/ProActiveOnloadList.cpp:135: error: (Each undeclared identifier is reported only once for each function it appears in.)
ngonl/src/bom/ProActiveOnloadList.cpp:135: error: expected `;' before '{' token
ngonl/src/bom/ProActiveOnloadList.cpp:135: error: expected `;' before '{' token

</std::pair></std::pair></unnamed></std::pair></std::pair></unnamed>


Why does it think I am passing in 3 arguments and what should the correct syntax be to get my iterator correct?


I know the vector is empty but I just want the syntax right on the BOOST_FOREACH loop.

I should add that if I have

std::vector<std::pair <int, int> >::iterator  anything;


it compiles fine but I think on BOOST_FOREACH you don't include the std::vector bit.

Thank you for any input
Posted
Updated 4-Nov-11 1:53am
v4

Just for a syntax test :) :
C++
std::vector < std::pair < int, int > > aa(/*..*/);
BOOST_FOREACH(std::pair < int, int >/*&*/ pair, aa)
{
  std::cout  << "hello";
}
 
Share this answer
 
I managed to get it to work...

<pre lang="c++">
std::vector<std::pair <int, int> > aa;
// std::vector<std::pair <int, int> >::iterator afasdf;
std::pair < int, int > pp ;
BOOST_FOREACH(pp , aa)
{
std::cout << "hello";
}


</pre>

but I dont know why it works.....

Still I can plod on with my coding.
 
Share this answer
 
Have you checked out this link on Boost:

boost foreach[^]

Otherwise, in your second entry, the foreach command needs a variable to receive the output from your container, as well as the instance of the container. You have declared a pair<> object pp to be the variable, and accept each value iterated in the vector aa.

If you want to modify the values in your vector, you would need to use a reference, &pp.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900