Search code examples
c++stackifstreamdynamic-arrays

Error: request for member 'insert' which is of non-class type int* ?? What's wrong?


This is the error I'm getting:

StatisticsProcessor.cpp: In member function ‘void StatisticsProcessor::loadData(std::string)’: StatisticsProcessor.cpp:70:16: error: request for member ‘insert’ in ‘((StatisticsProcessor*)this)->StatisticsProcessor::dataArray’, which is of non-class type ‘Element* {aka int*}’ make: * [StatisticsProcessor.o] Error 1

Here is that part of my code:

if(inStream.is_open()){
    for(int i = 0;;){
        if(!inStream.eof()){
            Element line = 0;
                if(i > 0){
                    inStream >> line;
                    dataArray.insert(line);
                }
                else{
                    dataArray = new(nothrow) Element[line];
                    assert(dataArray != 0);
                }
        }
        else if(inStream.eof()) break;
    }
            // Close file stream
            inStream.close();
}

and here is my insert function:

void StatisticsProcessor::insert(Element e){

    // Increment size
    size++;

    // Add in new value
    dataArray[size-1] = e;
}

Element is just a typedef for int, and dataArray is of type Element*
inStream is an ifstream object, I'm trying to read numbers from a file and insert them into my list class(essentially an array of integers). What am i doing wrong??


Solution

  • As you've shown us, the insert is a member function of the StatisticsProcessor class. If you want to call that function, the syntax would be:

    x.insert(y);
    

    Where x is an object of type StatisticsProcessor and y is an object of type Element (int). Instead, your x is an int* (pointer to int). Pointers don't have a member function named insert, nor any member functions at all, for that matter.