g ++错误:'malloc'未在此范围内声明 [英] g++ error: ‘malloc’ was not declared in this scope

查看:655
本文介绍了g ++错误:'malloc'未在此范围内声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Fedora下使用g ++来编译一个openGL项目,它具有以下行:

  textureImage =(GLubyte * *)malloc的(的sizeof(GLubyte *)* RESOURCE_LENGTH); 

编译时,g ++错误表示:

 错误:'malloc'未在此范围内声明

添加 #include< cstdlib> 并不能解决这个错误。 我的g ++版本是: g ++(GCC)4.4.5 20101112(Red Hat 4.4.5-2)

解决方案

<你应该在C ++代码中使用 new 而不是 malloc ,所以它变成新的GLubyte *改为[RESOURCE_LENGTH] 。当 #include > 时,它会将 malloc 加载到命名空间 std ,所以引用 std :: malloc (或者 #include 代替) 。


I'm using g++ under Fedora to compile an openGL project, which has the line:

textureImage = (GLubyte**)malloc(sizeof(GLubyte*)*RESOURCE_LENGTH);

When compiling, g++ error says:

error: ‘malloc’ was not declared in this scope

Adding #include <cstdlib> doesn't fix the error.

My g++ version is: g++ (GCC) 4.4.5 20101112 (Red Hat 4.4.5-2)

解决方案

You should use new in C++ code rather than malloc so it becomes new GLubyte*[RESOURCE_LENGTH] instead. When you #include <cstdlib> it will load malloc into namespace std, so refer to std::malloc (or #include <stdlib.h> instead).

这篇关于g ++错误:'malloc'未在此范围内声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆