-2

I have simple question. According to other examples my iteration for is correct but Eclipse throws me errors... My Function

billing::billing(std::istream &is) {
    unsigned day;
    std::string number;
    float time;
    struct call call;
    while (!is.eof()) {
        is >> day >> number >> time;
        call.day = day;
        call.number = number;
        call.time = time;
        blng_.push_back(call);
    }
    for(std::vector<call>::const_iterator it; it = blng_.begin(); it != blng_.end(); ++it)
        // THROWS HERE ERRORS!
        std::cout << it->day << std::endl;
}

After compiling he throws me something like that

expected a type, got 'call' billing.cpp     
'it' was not declared in this scope billing.cpp 
expected ';' before 'it'    billing.cpp 
expected ')' before ';' token   billing.cpp
invalid type in declaration before 'it' billing.cpp
template argument 2 is invalid  billing.cpp
the value of 'call' is not usable in a constant expression  billing.cpp
type/value mismatch at argument 1 in template parameter list for           
'template<class _Tp, class _Alloc> class std::vector'   billing.cpp

According to this How do I iterate over a Constant Vector? topic it should be working but it isn't and I have no freq idea why. When i change this whole std::vectro... to auto it works!

Community
  • 1
  • 1
FilOle
  • 53
  • 1
  • 6

1 Answers1

1

In C++11 you can rewrite:

for(std::vector<call>::const_iterator it = blng_.begin(); it != blng_.end(); ++it)
    std::cout<<it->day<<std::endl;;

as

for(const auto& c: blng_)
    std::cout << c.day << std::endl;

Additional Note:

You should never loop with eof(): Why is iostream::eof inside a loop condition considered wrong?

You should really do something like this:

while(is >> day >> number >> time) {

    call.day = day;
    call.number = number;
    call.time = time;
    blng_.push_back(call);
}
Community
  • 1
  • 1
Galik
  • 42,526
  • 3
  • 76
  • 100
  • Oh ye I was translating some parts of code to English and copied it wrong. But the problem was as juanchopanza wrote above. Thanks anyway! :) I rly like the last one! Galik Hmm and thanks for EOF note i will read it right away! – FilOle Apr 06 '15 at 22:04