问题描述
我正在编写一个程序,它应该使用矩阵操作,并使用OpenGL框架来绘制它们。老实说,这只是为了练习。无论如何,我试图编译它,当然,它没有运行良好。插入尽可能多的日志记录,我发现我的 std :: cerr
不能打印 float
值。我真的不知道如何可能停止工作。我对我的小代码做了一些深入的研究,如用它们的值替换变量,使用相同的函数编写外部程序等;发现更令人难以置信的事情,这绝对没有理由这样。
有用的细节
OS X 10.9。
除了由 #pragma once
引起的预编译之外,不会发生预编译的错误。
编译器
Apple LLVM版本6.0(clang-600.0.57)(基于LLVM 3.5svn)。
g ++ -std = c ++ 11 -framework OpenGL -framework GLUT
main.cpp
#include< GLUT / glut.h>
#include< cstdlib>
#includeWindow.hpp
Window * black_screen;
void Display(){black_screen-> Display(); }
void Reshape(int NEW_WIDTH,int NEW_HEIGHT){black_screen-> Reshape(NEW_WIDTH,NEW_HEIGHT); }
void Keyboard(unsigned char key,int x,int y){black_screen-> Keyboard(key,x,y); }
void Special(int key,int x,int y){black_screen-> Special(key,x,y); }
int main(int argc,char ** argv){
glutInit(& argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
窗口* black_screen =新窗口(800,800);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glutDisplayFunc(Display);
glutReshapeFunc(Reshape);
glutKeyboardFunc(Keyboard);
glutSpecialFunc(Special);
black_screen-> AddMatrix(Matrix(5));
Display();
glutMainLoop();
}
WINDOW.hpp
#pragma once
#include< GLUT / glut.h>
#include< cstdlib>
#include< iostream>
#include< string>
#includeMatrix.hpp
class Window {
std :: vector< Matrix> matrices_;
浮动WIDTH_,HEIGHT_,
SIZE_X_,SIZE_Y_;
const char * NAME_;
public:
Window(const float WIDTH,const float HEIGHT);
private:
void类型(const float x,const float y,const char * string)const;
public:
void AddMatrix(const Matrix& A);
void Write(const std :: string& message);
void Display();
void Reshape(int NEW_WIDTH,int NEW_HEIGHT);
void Keyboard(unsigned char key,int x,int y);
void Special(int key,int x,int y);
};
WINDOW.cpp
// --- THIS IS CONSTRUCTOR --- --- --- --- ---
Window :: Window(const float WIDTH,const float HEIGHT) :
WIDTH_(WIDTH),HEIGHT_(HEIGHT),
SIZE_X_(800),SIZE_Y_(800),
NAME _(MATRIX){
...
}
// ---这是显示功能从MAIN :: Display()---
调用void Window :: Display(){
glLoadIdentity(); Write(glLoadIdentity(););
glClear(GL_COLOR_BUFFER_BIT); Write(glClear(GL_COLOR_BUFFER_BIT););
glMatrixMode(GL_PROJECTION); Write(glMatrixMode(GL_PROJECTION););
Write(\tNOW i WILL TRY TO OUTPUT HEIGHT_:);
std :: cerr<< HEIGHT_<< std :: endl;
glOrtho(0,WIDTH_,0,HEIGHT_,1,-1); // Write(glOrtho(0,WIDTH_,0,HEIGHT_,1,-1););
...
}
输出
[glutInitWindowSize(SIZE_X_,SIZE_Y_); :
[SIZE_X_ 800.000000
[SIZE_Y_ 800.000000
[glutCreateWindow(NAME_); :
[NAME_ MATRIX
[glLoadIdentity();
[glClear(GL_COLOR_BUFFER_BIT);
[glMatrixMode(GL_PROJECTION);
[现在我将尝试输出HEIGHT_:
make:*** [run]分段错误:11
您不会在已发布的代码中显示此信息,但您似乎对 black_screen
,一个全局变量和一个 main
函数。 main
中的一个已经正确初始化,但是全局变量是一个坏指针。分段错误是通过取消引用错误指针的未定义行为的结果。
I am writing a program that is supposed to operate with matrices and to use OpenGL framework for drawing them. Honestly, it's just for practice. Anyway, I tried to compile it and, of course, it didn't run well. Inserting as many logging as I could, I found out that my std::cerr
can't print float
values. I really got no idea how that may stop working. I have made some deep researches on my little code like replacing variables with their values, coding external programs using same functions and so on; and found even more unbelievable thing that there's absolutely no reason for this.
Useful details
OS X 10.9.No bugs regarding to precompilation may occur other than ones caused by #pragma once
.
Apple LLVM version 6.0 (clang-600.0.57) (based on LLVM 3.5svn).
g++ -std=c++11 -framework OpenGL -framework GLUT
main.cpp
#include <GLUT/glut.h>
#include <cstdlib>
#include "Window.hpp"
Window *black_screen;
void Display () { black_screen->Display (); }
void Reshape (int NEW_WIDTH, int NEW_HEIGHT) { black_screen->Reshape (NEW_WIDTH, NEW_HEIGHT); }
void Keyboard (unsigned char key, int x, int y) { black_screen->Keyboard (key, x, y); }
void Special (int key, int x, int y) { black_screen->Special (key, x, y); }
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
Window *black_screen = new Window(800, 800);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glutDisplayFunc ( Display );
glutReshapeFunc ( Reshape );
glutKeyboardFunc ( Keyboard );
glutSpecialFunc ( Special );
black_screen->AddMatrix(Matrix(5));
Display();
glutMainLoop();
}
WINDOW.hpp
#pragma once
#include <GLUT/glut.h>
#include <cstdlib>
#include <iostream>
#include <string>
#include "Matrix.hpp"
class Window {
std::vector <Matrix> matrices_;
float WIDTH_, HEIGHT_,
SIZE_X_, SIZE_Y_;
const char *NAME_;
public:
Window (const float WIDTH, const float HEIGHT);
private:
void Type (const float x, const float y, const char *string) const;
public:
void AddMatrix (const Matrix &A);
void Write (const std::string &message);
void Display ();
void Reshape (int NEW_WIDTH, int NEW_HEIGHT);
void Keyboard (unsigned char key, int x, int y);
void Special (int key, int x, int y);
};
WINDOW.cpp
// --- THIS IS CONSTRUCTOR --- --- --- --- ---
Window::Window (const float WIDTH, const float HEIGHT) :
WIDTH_(WIDTH), HEIGHT_(HEIGHT),
SIZE_X_(800), SIZE_Y_(800),
NAME_("MATRIX") {
...
}
// --- THIS IS DISPLAY FUNCTION CALLED FROM MAIN::Display() ---
void Window::Display () {
glLoadIdentity(); Write("glLoadIdentity();");
glClear(GL_COLOR_BUFFER_BIT); Write("glClear(GL_COLOR_BUFFER_BIT);");
glMatrixMode(GL_PROJECTION); Write("glMatrixMode(GL_PROJECTION);");
Write("\tNOW i WILL TRY TO OUTPUT HEIGHT_ :");
std::cerr << HEIGHT_ << std::endl;
glOrtho(0, WIDTH_, 0, HEIGHT_, 1, -1); //Write("glOrtho(0, WIDTH_, 0, HEIGHT_, 1, -1);");
...
}
output
[ glutInitWindowSize(SIZE_X_, SIZE_Y_); :
[ SIZE_X_ 800.000000
[ SIZE_Y_ 800.000000
[ glutCreateWindow(NAME_); :
[ NAME_ MATRIX
[ glLoadIdentity();
[ glClear(GL_COLOR_BUFFER_BIT);
[ glMatrixMode(GL_PROJECTION);
[ NOW i WILL TRY TO OUTPUT HEIGHT_ :
make: *** [run] Segmentation fault: 11
You don't show this in the code posted, but it appears you have two different definitions for black_screen
, one global and one inside the main
function. The one inside main
has been initialized properly, but the global one is a bad pointer. The segmentation fault is the result of undefined behavior from dereferencing the bad pointer.
这篇关于std :: cerr不能打印浮点变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!