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

时间:2014-12-14 12:00:14

标签: c++ scope malloc 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;
    }   

};

1 个答案:

答案 0 :(得分:4)

std::calloc函数在<cstdlib>中定义。您需要包含它才能解决此错误。

有了这个说法,你最好使用operator new - 一种在C ++中分配动态内存的惯用方法。

使用std::vector而不是使用原始指针和new,你会更好(更好)。