Ben
Ben

Reputation: 4279

Pointer in a pair in C++

I need to return an array and it's size:

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);
}

The error output is:

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

How do I do it?

Upvotes: 0

Views: 3047

Answers (1)

zch
zch

Reputation: 15278

Since NULL is probably defined as:

#define NULL 0

the expression make_pair(NULL,-1) becomes make_pair(0, -1), so it creates pair<int, int>. You can for example use nullptr if available or (int*)NULL otherwise.

Upvotes: 3

Related Questions