1. <legend id='cLODz'><style id='cLODz'><dir id='cLODz'><q id='cLODz'></q></dir></style></legend>
        <bdo id='cLODz'></bdo><ul id='cLODz'></ul>

        <small id='cLODz'></small><noframes id='cLODz'>

        <tfoot id='cLODz'></tfoot>

      1. <i id='cLODz'><tr id='cLODz'><dt id='cLODz'><q id='cLODz'><span id='cLODz'><b id='cLODz'><form id='cLODz'><ins id='cLODz'></ins><ul id='cLODz'></ul><sub id='cLODz'></sub></form><legend id='cLODz'></legend><bdo id='cLODz'><pre id='cLODz'><center id='cLODz'></center></pre></bdo></b><th id='cLODz'></th></span></q></dt></tr></i><div id='cLODz'><tfoot id='cLODz'></tfoot><dl id='cLODz'><fieldset id='cLODz'></fieldset></dl></div>

        错误 C2228:“.size"的左边必须有类/结构/联合

        error C2228: left of #39;.size#39; must have class/struct/union(错误 C2228:“.size的左边必须有类/结构/联合)
          <tbody id='IIyCX'></tbody>
        <legend id='IIyCX'><style id='IIyCX'><dir id='IIyCX'><q id='IIyCX'></q></dir></style></legend>
          <bdo id='IIyCX'></bdo><ul id='IIyCX'></ul>

              <i id='IIyCX'><tr id='IIyCX'><dt id='IIyCX'><q id='IIyCX'><span id='IIyCX'><b id='IIyCX'><form id='IIyCX'><ins id='IIyCX'></ins><ul id='IIyCX'></ul><sub id='IIyCX'></sub></form><legend id='IIyCX'></legend><bdo id='IIyCX'><pre id='IIyCX'><center id='IIyCX'></center></pre></bdo></b><th id='IIyCX'></th></span></q></dt></tr></i><div id='IIyCX'><tfoot id='IIyCX'></tfoot><dl id='IIyCX'><fieldset id='IIyCX'></fieldset></dl></div>

              <tfoot id='IIyCX'></tfoot>
              • <small id='IIyCX'></small><noframes id='IIyCX'>

                  本文介绍了错误 C2228:“.size"的左边必须有类/结构/联合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在调用 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. 
                  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;
                  }
                  

                  推荐答案

                  信不信由你,这一行并没有声明一个名为std::vector的实例code>results,使用开始和结束迭代器调用构造函数:

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

                  这实际上声明了一个名为results的函数,它接受一个名为actualLineStream的参数和另一个未命名的参数,两者都是std类型::istream_iterator.

                  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 向后兼容——但这太违反直觉了,它甚至有自己的名字:"最令人烦恼的解析".一些编译器甚至会在遇到最烦人的解析时发出警告.

                  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"的左边必须有类/结构/联合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  C++ stl unordered_map implementation, reference validity(C++ stl unordered_map 实现,参考有效性)
                  C++: Is it possible to use a reference as the value in a map?(C++:是否可以使用引用作为映射中的值?)
                  Where ampersand quot;amp;quot; can be put when passing argument by reference?(其中符号“amp;通过引用传递参数时可以放置吗?)
                  Why can a non-const reference parameter be bound to a temporary object?(为什么可以将非常量引用参数绑定到临时对象?)
                  What is a dangling reference?(什么是悬空引用?)
                  C++ reference changes when push_back new element to std::vector(当 push_back 新元素到 std::vector 时,C++ 引用发生变化)
                    <tbody id='woSbU'></tbody>
                1. <tfoot id='woSbU'></tfoot>
                2. <legend id='woSbU'><style id='woSbU'><dir id='woSbU'><q id='woSbU'></q></dir></style></legend>

                    <i id='woSbU'><tr id='woSbU'><dt id='woSbU'><q id='woSbU'><span id='woSbU'><b id='woSbU'><form id='woSbU'><ins id='woSbU'></ins><ul id='woSbU'></ul><sub id='woSbU'></sub></form><legend id='woSbU'></legend><bdo id='woSbU'><pre id='woSbU'><center id='woSbU'></center></pre></bdo></b><th id='woSbU'></th></span></q></dt></tr></i><div id='woSbU'><tfoot id='woSbU'></tfoot><dl id='woSbU'><fieldset id='woSbU'></fieldset></dl></div>
                  1. <small id='woSbU'></small><noframes id='woSbU'>

                      <bdo id='woSbU'></bdo><ul id='woSbU'></ul>