0

Hey guys I'm having trouble with some code I'm writing for a .obj model parser. Here's the code that's causing the problem:

std::istringstream iss(line.substr(1));

std::copy(   
std::istream_iterator<float>(iss),     
std::istream_iterator<float>(),    
std::back_inserter<std::vector<float>>(model.chunks.back().vectices)  
);

It's basically taking a string passed as a argument like this:

v -5.000000 -1.000000 1.000000

Then gets the substring from it so it's left with only this:

-5.000000 -1.000000 1.000000

Finally I use std::copy and get each set of numbers from within the string:

vertices[0] = -5.000000
vertices[1] = -1.000000
vertices[2] = 1.000000

Anyways the main problem here is that I'm getting an error from this line of code:

std::back_inserter<std::vector<float>>(model.chunks.back().vectices));

It says "expected token ';' got float" my code still compiles and run's flawlessly though.

Although if I replace the floats in that code with std::string's I don't get the error anymore.

std::copy(  
std::istream_iterator<std::string>(iss),   
std::istream_iterator<std::string>(),   
std::back_inserter<std::vector<std::string>>(model.chunks.back().vectices)  
);

I'm using Qt Creator so could this possibly be just a IDE error? Any help would be greatly appreciated!

3
  • std::back_inserter can deduce type from argument. Commented Aug 2, 2012 at 4:39
  • 1
    Why don't you just write std::back_inserter(model.chunks.back().vectices) and let the compiler deduce the type argument? Commented Aug 2, 2012 at 4:59
  • Hmm.. didn't think of that. It also requires less code. Thanks for the tip! Commented Aug 2, 2012 at 5:05

1 Answer 1

4

It might be that your IDE doesn't understand the >> in your template specification. Older versions of the C++ standard required that you put a space in between each > as in > >, otherwise it could be confused with the right shift operator >>. So:

std::back_inserter<std::vector<float> >(model.chunks.back().vectices));
Sign up to request clarification or add additional context in comments.

3 Comments

Good theory, but he says it works with std::string in the last section of code, which includes the same >> sequence without spaces.
Ah, true. Curious that. But surely it's an IDE-specific thing, because otherwise code with syntax errors can't "compile and run flawlessly".
Just added a space in between the >> and the error went away! Must of been the IDE. Thanks you guys, and thank you for replying so quickly!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.