Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
146 views
in Technique[技术] by (71.8m points)

c++ - error C2228: left of '.size' must have class/struct/union

I'm getting this compiler error when calling vector's size(). Why?

#include <vector>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <cassert>


using namespace std;

class Vertex {

    float firstValue;
    float secondValue;
    float thirdValue;

    Vertex (float first, float second, float third){
          firstValue=first;
          secondValue=second;
          thirdValue=third;
    }

};


int main()
{
    cout<<"This program loads a 3D .off object. 
Enter the name of the file that describes it "<<endl;
    string inputFileName;
    getline(cin, inputFileName);

    ifstream inputFileStream;

    inputFileStream.open(inputFileName.data());
    assert (inputFileStream.is_open());

    string actualLine;

    for(;;){

        inputFileStream>>actualLine;

        istringstream actualLineStream(actualLine);



        std::vector<float> results( std::istream_iterator<int>(actualLineStream)
                        , std::istream_iterator<int>() );

       int resultsIndex=0;
       int resultsSize=results.size(); //WHY??

       while (resultsIndex<resultsSize){

         cout<<results[resultsIndex]<<endl;
       }


        if (inputFileStream.eof()) break;

    }


    ofstream outputChannel;

    while (true){} // to keep on console view 
    return 0;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Believe it or not, this line does not declare an instance of std::vector named results, calling the constructor taking a begin and end iterator:

std::vector<float> results(std::istream_iterator<int>(actualLineStream),
    std::istream_iterator<int>());

This actually declares a function called results that takes a parameter named actualLineStream and another unnamed parameter, both of type std::istream_iterator<int>.

Generally in C++, if something looks like a function, it will be parsed like one; the C++ standard requires it. This is really for backward compatibility with C - but this is so counterintuitive that it even has its own name: the "most vexing parse". Some compilers will even issue a warning if it encounters the most vexing parse.

It is related to the fact that these two lines are not equivalent in C++:

Foo bar;   // Declares an instance of Foo named bar
Foo bar(); // Declares a function named bar that takes no parameters and returns a Foo

To fix it, you can add more parentheses around one of the arguments:

//                         +--------- Note extra parentheses!! ---------+
//                         |                                            |
//                         V                                            V
std::vector<float> results((std::istream_iterator<int>(actualLineStream)),
    std::istream_iterator<int>());

Or simply declare each iterator separately:

std::istream_iterator<int> resultsBegin(actualLineStream);
std::istream_iterator<int> resultsEnd;
std::vector<float> results(resultsBegin, resultsEnd);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...