错误C2228:“。size”的左侧必须具有class / struct / union [英] error C2228: left of '.size' must have class/struct/union

查看:125
本文介绍了错误C2228:“。size”的左侧必须具有class / struct / union的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

调用vector的 size()时出现此编译器错误。为什么?

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. \nEnter 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;
}


推荐答案

信不信由你,这个第行声明名为结果 std :: vector 的实例,调用构造函数进行开始和结束迭代器:

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>());

这实际上是声明了一个函数,结果 ,它接受一个名为 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>.

通常在C ++中,如果某些东西看起来像一个函数,它将像一个函数一样被解析; C ++标准需要它。这实际上是为了与C向后兼容-但这违反直觉,以至于它甚至都有自己的名称: most烦人的解析 。一些编译器甚至会在遇到最棘手的解析时发出警告。

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.

这与以下事实有关,即这两行在C ++中并不等效:

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);

这篇关于错误C2228:“。size”的左侧必须具有class / struct / union的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆