C++ 错误 C2228:“.size”的左边必须有类/结构/联合
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4175971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
error C2228: left of '.size' must have class/struct/union
提问by andandandand
I'm getting this compiler error when calling vector's size()
. Why?
调用 vector 的size()
. 为什么?
#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;
}
回答by In silico
Believe it or not, this line does notdeclare an instance of std::vector
named results
, calling the constructor taking a begin and end iterator:
信不信由你,这一行并没有声明一个std::vector
named的实例,而是使用results
一个开始和结束迭代器调用构造函数:
std::vector<float> results(std::istream_iterator<int>(actualLineStream),
std::istream_iterator<int>());
This actually declares a functioncalled results
that takes a parameter named actualLineStream
and another unnamed parameter, both of type std::istream_iterator<int>
.
这实际上声明了一个被调用的函数results
,它接受一个名为 的参数actualLineStream
和另一个未命名的参数,两者都是类型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.
通常在 C++ 中,如果某个东西看起来像一个函数,它就会像一个函数一样被解析;C++ 标准要求它。这实际上是为了与 C 向后兼容——但这太违反直觉了,以至于它甚至有自己的名字:“最令人烦恼的解析”。一些编译器甚至会在遇到最烦人的解析时发出警告。
It is related to the fact that these two lines are not equivalent in C++:
这与这两行在 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);
回答by mkb
I think you hit this bugaboo. You have managed to declare results
as a new function that returns a std::vector<float>
. If I change the line that declares results
to:
我想你遇到了这个问题。您已成功声明results
为一个返回std::vector<float>
. 如果我将声明的行更改results
为:
std::vector<float> results = std::vector<float>( std::istream_iterator<int>(actualLineStream), std::istream_iterator<int>() );
I can compile this (with GCC, though, YMMV)
我可以编译这个(不过,使用 GCC,YMMV)