c++ - 错误: ‘calloc’ was not declared in this scope

标签 c++ scope malloc archlinux

我正在 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/

相关文章:

函数内的javascript函数

javascript - angular.module 和 window.angular.module 之间有什么区别吗?

c - 尝试释放分配的内存后挂起程序

c - 错误 "assignment makes integer from pointer without a cast array = NULL"

c++新运算符使用问题

C++ 循环 - 流错误?

c - 在 C 中使用作用域

c - 努力了解如何使用 malloc() 正确分配内存

c++ - 如何从两个字符串列表中删除公共(public)值

c++ - 构建失败 Netbeans 7.4 C/C++