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

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

  • <legend id='eaGW4'><style id='eaGW4'><dir id='eaGW4'><q id='eaGW4'></q></dir></style></legend>
        <bdo id='eaGW4'></bdo><ul id='eaGW4'></ul>

        为什么我不能把这个对象推到我的 std::list 上?

        Why can#39;t I push this object onto my std::list?(为什么我不能把这个对象推到我的 std::list 上?)

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

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

                  <tbody id='Hhtc5'></tbody>
                  <bdo id='Hhtc5'></bdo><ul id='Hhtc5'></ul>
                • 本文介绍了为什么我不能把这个对象推到我的 std::list 上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  刚开始用 C++ 编程.

                  Just started programming in C++.

                  我已经创建了一个 Point 类、一个 std::list 和一个迭代器,如下所示:

                  I've created a Point class, a std::list and an iterator like so:

                  class Point { 
                  public:
                      int x, y;
                      Point(int x1, int y1)
                      {
                          x = x1;
                          y = y1;
                      }
                  };
                  
                  std::list <Point> pointList;
                  std::list <Point>::iterator iter;
                  

                  然后我将新点推送到 pointList 上.

                  I then push new points onto pointList.

                  现在,我需要遍历 pointList 中的所有点,因此我需要使用迭代器进行循环.这就是我搞砸的地方.

                  Now, I'm needing to iterate through all the points in pointList, so I need to loop using the iterator. This is where I get screwed up.

                  for(iter = pointList.begin(); iter != pointList.end(); iter++)
                  {
                      Point currentPoint = *iter;
                      glVertex2i(currentPoint.x, currentPoint.y);
                  }
                  

                  你们是对的,问题不在于我迭代列表.问题似乎出在我试图将某些内容推送到列表时.

                  You guys were right, the problem isn't in my iterating the list. It appears the problem is when I am attempting to push something on to the list.

                  确切的错误:

                  mouse.cpp: 在函数 void mouseHandler(int, int, int, int)' 中:mouse.cpp:59: 错误:请求从Point*' 转换为非标量类型`Point'

                  mouse.cpp: In function void mouseHandler(int, int, int, int)': mouse.cpp:59: error: conversion fromPoint*' to non-scalar type `Point' requested

                  那些行是:

                   if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
                  {
                      Point currentPoint = new Point(x, y);
                      pointList.push_front(currentPoint);
                  
                  }
                  

                  Point* 和非标量类型 Point 之间的转换是什么?我只是想创建新的点并将它们推到这里的列表中.

                  What does it conversion between Point* to non-scalar type Point? I'm just trying to create new points and push them onto the list here.

                  推荐答案

                  那应该是一段有效的代码.

                  That should be a valid bit of code.

                  #include <iostream>
                  #include <list>
                  
                  class Point { 
                  public:
                      int x, y;
                      Point(int x1, int y1)
                      {
                          x = x1;
                          y = y1;
                      }
                  };
                  
                  int main()
                  {
                      std::list<Point> points;
                  
                      points.push_back(Point(0, 0));
                      points.push_back(Point(1, 1));
                      points.push_back(Point(2, 2));
                  
                      std::list<Point>::iterator iter;
                  
                      for(iter = points.begin(); iter != points.end(); ++iter)
                      {
                          Point test = *iter;
                          std::cout << test.x << ", " << test.y << "; ";
                      }
                      std::cout << std::endl;
                  
                      return 0;
                  }
                  

                  使用此代码:

                  jasons-macbook41:~ g++ test.cpp
                  jasons-macbook41:~ ./a.out
                  0, 0; 1, 1; 2, 2; 
                  jasons-macbook41:~ 
                  

                  尽管我不会像您的代码那样创建 Point 的临时副本.我会像这样重写循环:

                  Although I wouldn't create a temporary copy of the Point as your code does. I'd rewrite the loop like this:

                  for(iter = points.begin(); iter != points.end(); ++iter)
                  {
                      std::cout << iter->x << ", " << iter->y << "; ";
                  }
                  

                  迭代器在语法上类似于指针.

                  An iterator is syntactically similar to a pointer.

                  鉴于您的新问题,请从构造线上删除新".那是创建一个指向 Point 的指针,而不是堆栈上的 Point.这将是有效的:

                  Given your new problem, drop the "new" from the construction line. That's creating a pointer to a Point, as opposed to a Point on the stack. This would be valid:

                  Point* temp = new Point(0, 0);
                  

                  或者这个:

                  Point temp = Point(0, 0);
                  

                  你最好选择后者.

                  这篇关于为什么我不能把这个对象推到我的 std::list 上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Is Type(::x); valid?(是类型(::x);有效的?)
                  Difference between an inline function and static inline function(内联函数和静态内联函数的区别)
                  Compilation fails randomly: quot;cannot open program databasequot;(编译随机失败:“无法打开程序数据库)
                  Too many initializers error for a simple array in bcc32(bcc32 中的简单数组的初始值设定项过多错误)
                  No Member named stoi in namespace std(命名空间 std 中没有名为 stoi 的成员)
                  Error using a constexpr as a template parameter within the same class(在同一个类中使用 constexpr 作为模板参数时出错)
                      <tbody id='o07dp'></tbody>

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

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

                          <tfoot id='o07dp'></tfoot>