c++ - C++中成对的指针

标签 c++ pointers std-pair

我需要返回一个数组及其大小:

pair<int*, int> load(string path,int * memory){
    ifstream handle;
    int container;
    int size = 0; 

    if(!handle){
        QMessageBox::warning(0, QString(""), "file cannot be loaded");
        return make_pair(NULL,-1);
    }

    handle.open(path.c_str());
    while(!handle.eof()){
        handle >> container;
        size++;
    }
    size--;
    if(!size){
        QMessageBox::warning(0, QString(""), "file is empty");
        return make_pair(NULL,-1);
    }
    memory = new int[size]; 

    handle.clear();
    handle.seekg(0, ios::beg);

    for(int i = 0; i < size; i++){
        handle >> memory[i];
    }
    handle.close();
    return make_pair(memory, size);
}

错误输出为:

/usr/include/c++/4.6/bits/stl_pair.h:109: error: invalid conversion from 'int' to 'int*' [-fpermissive]

我该怎么做?

最佳答案

因为 NULL 可能被定义为:

#define NULL 0

表达式make_pair(NULL,-1)变成 make_pair(0, -1) , 所以它创建了 pair<int, int> .例如,您可以使用 nullptr如果可用或 (int*)NULL否则。

关于c++ - C++中成对的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14409170/

相关文章:

c++ - "Cannot appear in a constant expression",我需要它作为一个变量,为什么它不让我这样做?

c++ - 做std::pair a custom class的第二个参数

c++ - 如何将 std::iota 与 std::pair 一起使用?

c++ - "int(i)=1;"是什么意思?

c++ - 为什么我的代码两次打印相同的命令行参数?

c++ - C++11 之前的 "Constant expressions"

c++ - 在 std::pair 中存储不可复制(但可 move )的对象

c - 如何使用C中的指针在结构类型数组中获取字符串输入

c++ - 在 Clang 中禁用 "cast from pointer to smaller type uint32_t"错误

c - 使用双指针切换变量的值