代码无法运行 [英] Code fails to run
本文介绍了代码无法运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我尝试在VC 9.0中运行此代码时,出现编译或运行时错误.有人可以看到我滑倒的地方吗?
I have a compilation or runtime error when I try to run this code in VC 9.0. Can anybody see where I''ve slipped up?
// Code from Figs. 3.25 3.26 3.27 3.28
// and page 101 of Hill, F.S. "Computer Graphics Using
// OpenGL", 2nd edition, Prentice Hall, NJ, 2001.
// Modified Sept. 2002 by B.G. Nickerson into more
// structured C++ code.
// Code from Figs. 3.25 3.26 3.27 3.28
// and page 101 of Hill, F.S. "Computer Graphics Using
// OpenGL", 2nd edition, Prentice Hall, NJ, 2001.
// Modified Sept. 2002 by B.G. Nickerson into more
// structured C++ code.
#include<glut.h>
#include<math.h>
#include<iostream.h>
#include <stdlib.h>
using namespace std;
class Point2 //single point w/ floating point coordinates
{
public:
Point2() {x = y = 0.0f;} //constructor 1
Point2(float xx, float yy) {x=xx; y=yy;} //constructor 2
void set(float xx, float yy) {x=xx; y=yy;}
float getX() {return x;}
float getY() {return y;}
void draw(void)
{
glBegin(GL_POINTS); //draw this point
glVertex2f((GLfloat)x, (GLfloat)y);
glEnd();
}
private:
float x, y;
};
//<<Support Classes for Canvas>>>
class IntRect //aligned rectangle with integer coordinates, used for viewport
{
public:
IntRect() {l = 0; r=100; b=0; t=100;} //constructors
IntRect(int left, int right, int bottom, int top)
{l = left; r=right; b=bottom; t=top;}
void set(int left, int right, int bottom, int top)
{
l=left; r=right; b=bottom; t=top;
}
void draw(void); //draw this rectangle using OpenGL
int getL(void)
{
return l;
}
int getR(void)
{
return r;
}
int getT(void)
{
return t;
}
int getB(void)
{
return b;
}
private:
int l, r, b, t;
};
class RealRect //simlar to IntRect but w/ floating points & used for world window
{
public:
RealRect() {l = 0; r=100; b=0; t=100;} //constructors
RealRect(float left, float right, float bottom, float top)
{l = left; r=right; b=bottom; t=top;}
void set(float left, float right, float bottom, float top)
{
l=left; r=right; b=bottom; t=top;
}
float getL(void)
{
return l;
}
float getR(void)
{
return r;
}
float getT(void)
{
return t;
}
float getB(void)
{
return b;
}
void draw(void); //draw this rectangle using OpenGL
private:
float l, r, b, t;
};
//<<End Support Classes>>>
class Canvas
{
public:
Canvas(int width, int height, char* windowTitle); //constructor
void setWindow(float l, float r, float b, float t);
void setViewport(int l, int r, int b, int t);
IntRect getViewport(void); //divulge the viewport data
RealRect getWindow(void); // divulge the window data
float getWindowAspectRatio(void);
void clearScreen();
void setBackgroundColor(float r, float g, float b);
void setColor(float r, float g, float b);
void lineTo(float x, float y);
void lineTo(Point2 p);
void moveTo(float x, float y);
void moveTo(Point2 p);
void moveRel(float dx, float dy);
void turnTo(float angle);
void turn(float angle);
void forward(float dist, int isVisible);
Point2 Tween(Point2 A, Point2 B, float t);
void drawTween(Point2 A[], Point2 B[], int N, float t);
private:
Point2 CP; //current position in the world
IntRect viewport; //the current window
RealRect window; //the current viewport
float CD; //current direction in degrees (for turtle)
} ;
// Constructor
Canvas:: Canvas(int width, int height, char* windowTitle)
{
char* argv[1]; //dummy argument list for glutinit()
char dummyString[8];
argv[0] = dummyString; //hook up the pointer
int argc = 1;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(width, height);
glutInitWindowPosition (20,20);
glutCreateWindow (windowTitle);
setWindow(0, (float)width, 0, (float)height); // default world window
setViewport (0, width, 0, height); //default viewport
CP.set(0.0f, 0.0f); //initialize the cp to (0,0)
}
void Canvas:: moveTo(Point2 p) //moves current point CP to point p object
{
float x1, y1;
x1 = p.getX();
y1 = p.getY();
CP.set(x1, y1);
}
void Canvas:: forward(float dist, int isVisible) //moves turtle forward by dist
{
const float RadPerDeg=0.017453293; //radians per degree
float x = CP.getX() + dist * cos(RadPerDeg * CD);
float y = CP.getY() + dist * sin(RadPerDeg * CD);
if(isVisible)
lineTo(x, y);
else
moveTo(x, y);
}
void Canvas:: turnTo(float angle)
{
CD = angle;
}
void Canvas:: turn(float angle) {CD += angle;}
float Canvas:: getWindowAspectRatio(void) //calculates aspect ratio of world window
{
float width, height, aspectRatio;
width = window.getR() - window.getL();
height = window.getT() - window.getB();
aspectRatio = width/height;
return aspectRatio;
}
void Canvas:: moveRel(float dx, float dy)
{
CP.set(CP.getX() + dx, CP.getY() + dy);
}
//<<setWindow>>
void Canvas:: setWindow (float l, float r, float b, float t)
{
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
gluOrtho2D ((GLdouble)l, (GLdouble)r, (GLdouble)b, (GLdouble)t);
window.set (l, r, b, t);
}
//<<setViewport>>
void Canvas:: setViewport (int l, int r, int b, int t)
{
glViewport(l, b, r-l, t-b);
viewport.set (l, r, b, t);
}
RealRect Canvas:: getWindow(void)
{
return window;
}
IntRect Canvas:: getViewport(void)
{
return viewport;
}
void Canvas:: clearScreen(void)
{
glClear(GL_COLOR_BUFFER_BIT);
}
void Canvas:: setBackgroundColor(float r, float g, float b)
{
glClearColor(r, g, b, 0.0); //4th variable level of transparency, may need to change
}
void Canvas:: setColor(float r, float g, float b)
{
glColor3f(r, g, b);
}
void Canvas:: lineTo(Point2 p)
{
glBegin (GL_LINES);
glVertex2f((GLfloat) CP.getX(), (GLfloat) CP.getY());
glVertex2f((GLfloat) p.getX(), (GLfloat) p.getY());
glEnd();
CP.set(p.getX(), p.getY());
glFlush();
}
//<<moveTo>>
//changes current point
void Canvas:: moveTo(float x, float y)
{
CP.set(x, y);
}
//<<lineTo>>
//draws a line from current point to new point
void Canvas:: lineTo(float x, float y)
{
glBegin(GL_LINES);
glVertex2f((GLfloat) CP.getX(), (GLfloat) CP.getY());
glVertex2f((GLfloat) x, (GLfloat) y); //draw the line
glEnd();
CP.set (x, y); //update current point to new point
glFlush();
}
//<<drawTween>>
//draw the tween at time t between polylines A and B
Point2 Canvas:: Tween(Point2 A, Point2 B, float t)
{
Point2 P;
P.set(A.getX() + (B.getX() - A.getX())*t, A.getY() + (B.getY() - A.getY())*t);
return P;
}
//<<drawTween>>
//draw the tween at time t between polylines A and B
void Canvas:: drawTween(Point2 A[], Point2 B[], int N, float t)
{
for(int i = 0; i < N; i++)
{
Point2 P;
P = Tween(A[i], B[i], t);
if (i == 0) moveTo(P.getX(), P.getY());
else lineTo(P.getX(), P.getY());
}
}
// Code from Figs. 3.25 3.26 3.27 3.28
// and page 101 of Hill, F.S. "Computer Graphics Using
// OpenGL", 2nd edition, Prentice Hall, NJ, 2001.
// Modified Sept. 2002 by B.G. Nickerson into more
// structured C++ code.
// Code for example canvas, myDisplay, drawTween to
// interpolate between two polyline representations
// of equal length.
Canvas cvs(640, 640, "Example of tweening"); //global canvas object
void myDisplay(void)
{
cvs.setViewport(0, 640, 0, 640);
cvs.clearScreen();
cvs.setWindow(0.0, 1.0, 0.0, 1.0);
// Tween between two figures; a la Fig. 4.22, p.171
Point2 A[10];
Point2 B[10];
A[0].set(0.2, 0.5);
A[1].set(0.5, 0.35);
A[2].set(0.8, 0.5);
A[3].set(0.5, 0.2);
A[4].set(0.2, 0.5);
B[0].set(0.2, 0.2);
B[1].set(0.2, 0.8);
B[2].set(0.8, 0.8);
B[3].set(0.8, 0.2);
B[4].set(0.2, 0.2);
float t = 0.0;
int N = 10; // Number of tweenings
float tinc = 1.0/N;
int vpsize = 640/(N+1);
for (int i = 0; i <= N; i++)
{
cvs.setViewport(i*vpsize,(i+1)*vpsize, (N/2)*vpsize, (N/2+1)*vpsize);
cvs.drawTween(A, B, 5, t);
t = t + tinc;
}
}
// Code from Figs. 3.25 3.26 3.27 3.28
// and page 101 of Hill, F.S. "Computer Graphics Using
// OpenGL", 2nd edition, Prentice Hall, NJ, 2001.
// Modified Sept. 2002 by B.G. Nickerson into more
// structured C++ code.
// example of using canvas class with tweening.
int main(int argc, char **argv)
{
cvs.setWindow(-1.0, 1.0, -1.0, 1.0);
cvs.setViewport(0, 640, 0, 640);
cvs.setBackgroundColor(1.0, 1.0, 1.0);
cvs.setColor(0.0, 0.0, 0.0);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutDisplayFunc(myDisplay);
glutMainLoop();
return 0;
}
我从标准库头文件中收到退出错误.
错误报告:
i recieve exit error from standard library header file.
error report:
1>c:\program files (x86)\microsoft visual studio 9.0\vc\include\stdlib.h(371) : warning C4985: 'exit': attributes not present on previous declaration.
1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\gl\glut.h(146) : see declaration of 'exit'
推荐答案
您需要告诉我们为什么无法运行它.您安装了OpenGL吗?您会遇到什么错误?
majidsaba写道:
#include& lt; math.h& gt;
#include& lt; iostream.h& gt;
#include& lt; stdlib.h& gt;
据我记得,这些不是标准的C ++. iostream.h绝对不是C ++. VC现在非常符合标准,如果它坚持要求您使用不带.h的C ++版本,我不会感到惊讶.
You need to tell us why you can''t run it. Have you installed OpenGL ? What errors do you get ?
majidsaba wrote:
#include<math.h>
#include<iostream.h>
#include <stdlib.h>
These are not standard C++, as far as I recall. iostream.h is DEFINITELY not C++. VC is very standards compliant nowadays, I would not be surprised if it insisted you use the C++ versions, which don''t have the .h.
报告的消息不是错误,这是一个警告.
您可以查看两个头文件以检查不一致之处.
:)
The reported message is not an error, it is a warning.
You may have a look at both header files to check what is the inconsistency.
:)
majidsaba写道:
majidsaba wrote:
//B.G.于2002年9月修改.尼克森加入了更多//结构化的C ++代码.
// Modified Sept. 2002 by B.G. Nickerson into more// structured C++ code.
就像我说的那样,这段代码不是有效的C ++.您需要告诉我们
1-您已安装OpenGL
2-确切的错误消息是什么
3-哪行代码会产生错误
As I said, this code is not valid C++. You need to tell us
1 - that you installed OpenGL
2 - what the exact error message is
3 - what line of code generates the error
这篇关于代码无法运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文