我正在使用g++
arch linux
编译我的代码
编译时出现此错误。
.error: ‘calloc’ was not declared in this scope
我没有尝试在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;
}
};
答案 0 :(得分:4)
std::calloc
函数在<cstdlib>
中定义。您需要包含它才能解决此错误。
有了这个说法,你最好使用operator new
- 一种在C ++中分配动态内存的惯用方法。
使用std::vector
而不是使用原始指针和new
,你会更好(更好)。