BOOST_FOREACH problem

Hi!

A little problem with the BOOST_FOREACH function.

documentation here: http://www.boost.org/doc/libs/1_53_0/doc/html/foreach.html

code:
1
2
3
    BOOST_FOREACH(mi34.getBuyOrders()::value_type i&, mi34.getBuyOrders()){ //row 122
        cout << i.getPrice() << endl;
    }


I mean, it should do ok right? for each value in mi34.getBuyOrders() do{}

Errors:
1
2
3
4
main.cpp||In function 'int main()':|
main.cpp|122|error: expected ';' before '::' token|
main.cpp|123|error: 'i' was not declared in this scope|
||=== Build finished: 2 errors, 0 warnings (0 minutes, 1 seconds) ===|
What does construction mi34.getBuyOrders()::value_type mean?

mi34.getBuyOrders() is an expression. It is not a type name.
So what should I do instead?

doesn't mi34.getBuyOrders()::value_type return the type of the element? in this case mi34.getBuyOrders() so it should return vector<Order>& right?


getBuyOrders() returns a vector<Order>&


or wait...

ah well... I'm lost.
Last edited on
I do not know what mi34.getBuyOrders() returns. I only wonder why do you get into boost if you have no basic knowledge of C++?
I have some basic knowledge of C++.

And I use Boost since I used to to parse the XML document into my classes, and while in there I found the nice little function BOOST_FOREACH that allows me to iterate through all the elements much easier.

and getBuyOrders returns a vector<Order>&


and Order has a member function called getPrice() that returns a double.
Last edited on
Why do you not use the standard range-based for statement?

I think the same can be written as

for ( auto &order : mi34.getBuyOrders() ) cout << order.getPrice() << std::endl;
Nope, didn't work, and you don't have to declare it to auto since that's its default(as far as I know).

The errors:
1
2
3
4
5
6
In function 'int main()':|
\main.cpp|122|warning: 'auto' changes meaning in C++11; please remove it [-Wc++0x-compat]|
\main.cpp|122|error: ISO C++ forbids declaration of 'Order' with no type [-fpermissive]|
\main.cpp|122|error: range-based 'for' loops are not allowed in C++98 mode|
\main.cpp|122|error: request for member 'getPrice' in 'Order', which is of non-class type 'int'|
||=== Build finished: 3 errors, 1 warnings (0 minutes, 1 seconds) ===|
Last edited on
Pay attention Jonas!
Vlad is using two new features of C++11: range-based for() loops and automatic type inference. Read the error messages again.
@Jonas Wingren
Nope, didn't work, and you don't have to declare it to auto since that's its default(as far as I know).



You shall use the option of the compiler that allows to use C++ 11 features.
A function does not return a type. it returns an object of a specific type.


BOOST_FOREACH is a macro that has certain limits (especially without C++11)

What you can try is result_of:

file:///D:/Entwicklung/Extern/Bibliotheken/vc++2010/boost/libs/utility/utility.htm#result_of

1
2
3
BOOST_FOREACH(boost::result_of(mi34.getBuyOrders())::value_type &i, mi34.getBuyOrders()){ //Note i& -> &i
        cout << i.getPrice() << endl;
    }
ok, flagged it for C++ 11. Works now.

and thanks coder777
Topic archived. No new replies allowed.