用C ++编写的指针

问题描述 投票:0回答:1

我需要返回一个数组,它的大小:

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:错误:从'int'无效转换为'int *'[-fpermissive]

我该怎么做?

c++ pointers std-pair
1个回答
2
投票

由于NULL可能定义为:

#define NULL 0

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

© www.soinside.com 2019 - 2024. All rights reserved.