gpt4 book ai didi

c++ - `fopen` 和 QT(MinGW) 错误

转载 作者:行者123 更新时间:2023-11-30 05:43:57 25 4
gpt4 key购买 nike

我的 QT 应用程序中有以下代码:

FILE * file = fopen(m_fileName.c_str(), "rb");

,其中 m_fileName 的类型是 std::string。此代码在 Visual Studio2012 中运行良好。但是在 QT 4.7.4(MinGW 编译器)中,我的程序在这一行崩溃了。

我真的不知道这段代码有什么问题。我没怎么用过MinGW,所以肯定有一些我不知道的东西。

更新:

来自 main.cpp 的代码:

std::string fileName = "test1.bmp";
m_pTexture1 = new Texture(GL_TEXTURE_2D, fileName);
if (!m_pTexture1->Load()) {
return;
}

Texture.cpp的简化代码:

Texture::Texture(GLenum TextureTarget, std::string FileName)
{
m_textureTarget = TextureTarget;
m_fileName = FileName;
}

bool Texture::Load()
{
try {
FILE * file = fopen(m_fileName.c_str(),"rb");
}
catch (...) {
std::cout « "Error loading texture '" « m_fileName;
return false;
}
}

Texture.h代码:

class Texture
{
public:
Texture(GLenum TextureTarget, std::string FileName);
bool Load();

private:
std::string m_fileName;
GLenum m_textureTarget;
GLuint m_textureObj;
unsigned int width, height;
unsigned char * data;
};

是的,@PaulMcKenzie 是对的。我试图在构造函数中打印 m_fileName 并且它破坏了程序。看起来 m_fileName 无法初始化。但是,我不知道为什么会这样。

更新 2

我发现它因为 printf 和其他 C i/o 函数而崩溃。很奇怪。

最佳答案

您的代码存在语法错误。 (括号)

FILE * file = fopen(m_fileName.c_str()), "rb");
^
这个

编译成功了吗?

关于c++ - `fopen` 和 QT(MinGW) 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30021303/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com