0

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?

Brian Tompsett - 汤莱恩
  • 5,195
  • 62
  • 50
  • 120
Ben
  • 3,051
  • 5
  • 38
  • 70
  • 5
    Welcome to "C++ for the gentleman of class". May we interest you in our shiny `std::vector`s? They even know their own size! Guaranteed leak- and weather-proof. – Kerrek SB Jan 18 '13 at 23:16
  • 1
    But your question evaporates if you use vectors. Also, you have a massive `eof()` error in your code. – Kerrek SB Jan 18 '13 at 23:17
  • Everything. Literally. Search this site for thousands of questions with the same problem, or just stick around for half a day. There's basically no correct way to use `eof()`, and people who use it *always* get it wrong. – Kerrek SB Jan 18 '13 at 23:23
  • 1
    Question about `eof`: http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – zch Jan 18 '13 at 23:24
  • My bad. And what is the problem? The eof seems to have worked out for me. – Ben Jan 18 '13 at 23:25
  • Well, your code isn't serious. The `container` variable is just dead. It would be undefined behaviour to read it, though. – Kerrek SB Jan 18 '13 at 23:26
  • Well, i can't argue about it. I am a naive writer – Ben Jan 18 '13 at 23:31
  • @KerrekSB There are correct ways of using `eof()`. But only once you've determined that the input has failed. (If `eof()` and `bad()` aren't true, after failure, you have a format problem.) – James Kanze Jan 19 '13 at 01:06
  • @JamesKanze: That's true (and a good example of what it's useful for!), and I used to be more reserved in the way I phrased this. But in my experience since, anyone who posts on SO and uses `eof()` does it for the wrong reasons and in the wrong way. Presumably, once you were deliberately trying to tell formatting problems from end-of-input, you would already have bothered to read up the manual on what all those status bits do! :-) – Kerrek SB Jan 19 '13 at 10:42

1 Answers1

3

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.

zch
  • 14,151
  • 2
  • 39
  • 49