我試圖實施科赫的雪花。爲了練習,我列了一個通用列表,但我遇到了一些問題。列表中的模板錯誤
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <gl/glut.h>
template <typename T> class Node {
public:
T data;
Node<T> *next;
Node<T>(T d) {
next = NULL;
data = d;
}
};
template <typename T> class List {
Node<T> *head;
int size;
public:
List() {
head = NULL;
size = 0;
}
void append(T data){
if(head == NULL) {
head = new Node<T>(data);
} else {
Node<T> *n = head;
while(n->next != NULL) {
n = n->next;
}
n->next = new Node<T>(data);
}
size++;
}
void appendAll(List<T> data) {
if(data.getHead() == NULL)
return;
Node<T> *n = data.getHead();
append(n->data);
while(n->next != NULL){
append(n->next->data);
n = n->next;
}
}
Node<T>* getHead(){ return head; }
};
void myinit();
void display();
void draw_snowflake();
List<GLfloat[2]> divide_snowflake(GLfloat A[2], GLfloat B[2], int n);
GLfloat tri[3][2] = {{-1.0, -0.58}, {1.0, -0.58}, {0.0, 1.15}};
List<GLfloat[2]> snow;
int n;
int main(int argc, char **argv) {
n = 0;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500,500);
glutCreateWindow("Koch Snowflake");
glutDisplayFunc(display);
myinit();
glutMainLoop();
return EXIT_SUCCESS;
}
void myinit(){
// Initialize OpenGL
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-2.0, 2.0, -2.0, 2.0);
glMatrixMode(GL_MODELVIEW);
glClearColor(1.0, 1.0, 1.0, 1.0);
glColor3f(0.0,0.0,0.0);
// Initialize list of line_loop
snow.append(tri[0]);
snow.append(tri[1]);
snow.append(tri[2]);
}
void display(){
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_LINE_LOOP);
draw_snowflake();
glEnd();
glFlush();
}
void draw_snowflake(){
List<GLfloat[2]> temp;
temp.append(snow.getHead()->data);
Node<GLfloat[2]> *curr = snow.getHead();
while(curr->next != NULL) {
temp.appendAll(divide_snowflake(curr->data, curr->next->data, n));
temp.append(curr->next->data);
curr = curr->next;
}
temp.appendAll(divide_snowflake(curr->data, snow.getHead()->data, n));
Node<GLfloat[2]> *ptr = temp.getHead();
printf("\n>Drawing %f, %f", ptr->data[0], ptr->data[1]);
glVertex2fv(ptr->data);
while(ptr->next != NULL) {
printf("\n>Drawing %f, %f", ptr->next->data[0], ptr->next->data[1]);
glVertex2fv(ptr->next->data);
ptr = ptr->next;
}
}
List<GLfloat[2]> divide_snowflake(GLfloat A[2], GLfloat B[2], int n) {
GLfloat A_Mid[2] = {A[0] + (B[0] - A[0])/3,
A[1] + (B[1] - A[1])/3};
GLfloat Mid[2] = {A[0] + (B[0] - A[0])/2,
A[1] + (B[1] - A[1])/2};
GLfloat B_Mid[2] = {B[0] - (B[0] - A[0])/3,
B[1] - (B[1] - A[1])/3};
GLfloat Peak[2] = {Mid[0] + (Mid[1] - B_Mid[1]) * sqrt(3.0),
Mid[1] + (Mid[0] - A_Mid[0]) * sqrt(3.0)};
List<GLfloat[2]> temp;
if(n > 0) temp.appendAll(divide_snowflake(A, A_Mid, n-1));
temp.append(A_Mid);
if(n > 0) temp.appendAll(divide_snowflake(A_Mid, Peak, n-1));
temp.append(Peak);
if(n > 0) temp.appendAll(divide_snowflake(Peak, B_Mid, n-1));
temp.append(B_Mid);
if(n > 0) temp.appendAll(divide_snowflake(B_Mid, B, n-1));
return temp;
}
這裏是我得到的錯誤:
Error 1 error C2440: '=' : cannot convert from 'GLfloat []' to 'float [2]' 13
當我剛初始化爲列表<GLfloat*
>這將只設置節點的數據作爲單個值;而我想要點。出於實踐目的,我想繼續使用通用列表。
爲什麼不使用'std :: list'?這是一個通用的列表,它的工作原理。 – mfontanini 2012-07-10 22:48:22
@mfontanini:它說在最頂層的「練習」。如果每個程序員都默認從第1天開始圖書館的技術人員足夠熟練,可以繼續在將來編寫圖書館? – 2012-07-10 22:49:08
@EdS。哦,我總是想念:/。是的,我也創建了自己的結構。我只是以爲他只是在創建一個列表,因爲他想要一個「通用列表」。 – mfontanini 2012-07-10 22:49:46