我正在 arch linux
中使用 g++
编译我的代码
编译时出现此错误。
.error:“calloc”未在此范围内声明
我还没有尝试在 ubuntu
中编译此代码,但我非常确定它会通过。那么这是与 arch linux
相关的问题还是某些错误用我的代码
这是我的代码:
#include <cstdio>
using namespace std;
class Graph
{
private :
unsigned int numNodes;
class Connection
{
public :
int to;
int weight;
Connection (int to,int weight)
{
this->to = to;
this->weight = weight;
}
Connection (int to)
{
this->to = to;
}
};
Connection **nodeList;
public :
Graph (unsigned int numNodes)
{
this->nodeList = calloc (sizeof (Connection*),numNodes);
this->numNodes = numNodes;
}
};
最佳答案
std::calloc
函数定义于 <cstdlib>
。您需要包含它才能修复此错误。
话虽如此,您最好使用 operator new
- 在 C++ 中分配动态内存的惯用方法。
使用 std::vector
会让您的生活变得更好(更好)。而不是使用原始指针和 new
.
关于c++ - 错误: ‘calloc’ was not declared in this scope,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27469060/