Simon Knapp
Simon Knapp

Reputation: 303

Calling construct confused with function prototype

I'm having troubles with method1 below. It gives the warning prototyped function not called (was a variable definition intended?) at the line where I declare (and construct) vals. When the call to copy immediately below that is uncommented, I get error C2228 (left of '.begin' must have class/struct/union).

Googling yields solutions for the case of calling a default constructor (namely - remove the parentheses), but I am not using a default constructor and cannot figure out why this won't compile. Note that method2 compiles fine.

    static void method1(char const* filename) {
        vector<double> vals(istream_iterator<double>(ifstream(filename)), istream_iterator<double>());
        //copy(vals.begin(), vals.end(), ostream_iterator<double>(cout, ","));
    }

    static void method2(char const* filename) {
        vector<double> vals;
        vals = vector<double>(istream_iterator<double>(ifstream(filename)), istream_iterator<double>());
        copy(vals.begin(), vals.end(), ostream_iterator<double>(cout, ","));
    }

My questions are why and can anyone point to good information on why the compiler is confusing this variable declaration with a function prototype (I've written loads of code with, what looks to me like, exactly the same syntax and have never had this problem before)? Thanks!

I am using visual studio 2008.

Upvotes: 0

Views: 114

Answers (1)

Alok Save
Alok Save

Reputation: 206606

This is one of those quirks in C++ which you know only after you are bitten by it.
This is the most vexing parse in C++.

Upvotes: 3

Related Questions