为什么不能将此对象推送到std :: list上? [英] Why can't I push this object onto my std::list?

查看:103
本文介绍了为什么不能将此对象推送到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: error: conversion from 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上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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