我一直得到列表迭代器而不是dereferencable,我做错了什么?

时间:2016-04-22 13:22:02

标签: c++

我在使用这个项目时遇到了麻烦。我想要它绘制一个八边形,我使用的代码非常适合其他形状,如菱形和三角形。

octagon.cpp文件http://pastebin.com/iVfdkKEB

标题文件http://pastebin.com/a50UQi5F

运行它的主要部分http://pastebin.com/quepi6az

#include "shape.h"
class Octagon : public Shape
{
int radius;

void plotVertices();
public:
Octagon(Vertex point, int radius = 10);
int area();
int perimeter();
};

#include "octagon.h"

Octagon::Octagon(Vertex point, int radius) : Shape(point)
{
// constructs a Octagon of radius around a point in 2D space
if ((radius>centroid.getX() / 2) || (radius>centroid.getX() / 2))
{
    cout << "Object must fit on screen." << endl;
    system("pause");
    exit(0);
    this->radius = radius;
    plotVertices();
}
// place your code here and add comments that describe your understanding of what is happening

}
void Octagon::plotVertices()
{

int x, y, _x, _y; // declare and intiliase variables for x and y co-ordinates
double radians;

x = centroid.getX(); // places first point A at the centroid
y = centroid.getY() + radius;
vertices.push_back(Vertex(x, y));

x = vertices.back().getX() - centroid.getX();
y = vertices.back().getY() - centroid.getY();

for (int i = 45; i < 360; i += 45) // for loop to draw the shape itself by creating the points.
    // i = size of interior angle.
{
    radians = i * PI / 180;
    _x = round(x * cos(radians) - y * sin(radians));
    _y = round(y* cos(radians) + x * sin(radians));
    _x = _x + centroid.getX();
    _y = _y + centroid.getY();
    vertices.push_back(Vertex(_x, _y));
}
}


#pragma once

#include "vertex.h"
#include "shape.h"
#include "octagon.h"
#include "console.h"
#include <iostream>
#include <list>
#include <cmath>
#include <iostream>

using namespace std;

int main()
{
list<Shape*> shapes;

int x = 20, y = 60;
shapes.push_back(new Octagon(Vertex(20, 60), 8));

list<Shape*>::iterator itr = shapes.begin();
while(itr!=shapes.end())
{
    (*itr)->drawShape();
    system("pause");
    (*itr)->outputStatistics();
    // output shape statistics
    (*itr)->scale(2.0);
    (*itr)->drawShape();
    // scale shape (double it)
    // draw shape
    (*itr)->rotate(20);
    (*itr)->drawShape();
    // rotate shape by 20 degrees
    // draw shape
    itr++;
}

Console::gotoXY(0,0);
system("pause");
return 0;
}

和绘制形状函数

void Shape::drawShape()
{
// plots each vertex and draws a line between them using Bresenham's algorithm
// you can adjust your console font and dimensions if you want to increase the resolution of your shapes

list<Vertex>::iterator current = vertices.begin();
list<Vertex>::iterator previous = vertices.begin();
while (current != vertices.end())
{
    Console::gotoXY((*current).getX(), (*current).getY());
    cout << "*";
    if (current != vertices.begin())
        drawLine((*current).getX(), (*current).getY(), (*previous).getX(), (*previous).getY());
    previous = current;
    current++;
}
drawLine(vertices.back().getX(), vertices.back().getY(), vertices.front().getX(), vertices.front().getY());
}

1 个答案:

答案 0 :(得分:0)

我很不确定,但我认为问题可以在void Shape::drawShape()的最后一行

drawLine(vertices.back().getX(), vertices.back().getY(), vertices.front().getX(), vertices.front().getY());

其中不检查vertices是否为空。在这种情况下,vertices.back()vertices.front()未定义(均等于vertices.end()?)并取消引用它们,以致getX()getY(),可能会导致您的问题

可能是空的vertices?我不知道,因为我没有看到所有代码,但我发现exit(0)构造函数的if正文中有Octagon

plotVertices()填充vertices?在这种情况下,exit(0)告诉我们vertices为空,而drawline()中的最后Shape::drawshape可能会导致问题。

解决方案(解决方案)显而易见:检查vertices是否为空

if ( false == vertices.empty() )
   drawLine(vertices.back().getX(), vertices.back().getY(), vertices.front().getX(), vertices.front().getY());