std :: cerr无法打印float变量

时间:2015-04-21 21:12:01

标签: c++ c++11 osx-mavericks iostream

我正在编写一个应该与矩阵一起运行的程序,并使用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

1 个答案:

答案 0 :(得分:2)

您未在发布的代码中显示此内容,但似乎您对black_screen有两个不同的定义,一个是全局定义,另一个定义在main函数内。 main内的一个已经正确初始化,但全局一个是坏指针。分段错误是解除引用错误指针的未定义行为的结果。