std::cerr 无法打印浮点变量

std::cerr can't print float variable

我正在编写一个程序,该程序应该使用矩阵进行操作并使用 OpenGL 框架来绘制它们。老实说,这只是为了练习。不管怎样,我试着编译它,当然,它没有 运行 好。尽可能多地插入日志记录,我发现我的 std::cerr 无法打印 float 值。我真的不知道这怎么会停止工作。我对我的小代码进行了一些深入的研究,比如用它们的值替换变量,使用相同的函数编写外部程序等等;更令人难以置信的是,这完全没有理由。

有用的细节

OS X 10.9。 除了由 #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);");
    ...
}

输出

[   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

您没有在发布的代码中显示这一点,但您似乎对 black_screen 有两种不同的定义,一种是全局定义,另一种是在 main 函数中。 main 里面的指针已经正确初始化,但是全局指针是个坏指针。分段错误是取消引用坏指针导致的未定义行为的结果。