PRP*_*PRP 2 c++ malloc scope archlinux
我正在使用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;
}
};
Run Code Online (Sandbox Code Playgroud)
std::calloc函数在中定义<cstdlib>。您需要包括它才能修复此错误。
如此说来,您最好使用operator new-一种在C ++中分配动态内存的惯用方式。
而且您使用时会更好(更好) std::vector而不是使用原始指针和new。