2016-04-22 49 views
-4

我遇到了這個項目的麻煩。我想要它畫出一個八角形,我使用的代碼對菱形和三角形等其他形狀非常合適。我不斷收到列表迭代器不可忽略,我做錯了什麼?

的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()); 
} 
+0

請張貼您的代碼文本和你的問題提供了一個[MCVE。 – NathanOliver

+0

我看不到任何此代碼產生該錯誤的原因。請注意,你稱之爲上帝知道的一堆功能。出示您的[MCVE]。 –

+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?我不知道,因爲我沒有看到所有代碼,但是我看到在Octagon構造函數的if正文中有一個exit(0)

這是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()); 
相關問題