2
votes

So the nullptr error doesnt show up when i compile it at school and i think i can fix it with adding a line when i compile it, is there another way to get rid of it, and the other two errors i dont understand why im getting them at all. Can someone explain at least the nullptr error?

main.cpp: In function 'int main()':

error: 'array' was not declared in this scope

error: 'hours' was not declared in this scope

error: 'nullptr' was not declared in this scope

    int main()
{
    float *studentData;
    int numStudents;
    int size;
    int average = getAverage(*array, *hours);
    int median = getMedian(hours);
    int mode = getMode(hours);

    cout << "How many students were surveyed?  ";
    cin >> numStudents;
    studentData = makeArray(numStudents);

     if (studentData == nullptr)
         cout << "Error allocating memory.";
     else
     {
         getFBData(studentData, numStudents);
         selectionSort(studentData, numStudents);

         for (int i = 0; i < numStudents; i++)
             cout << *(studentData + i) << endl;

         delete[] studentData;
     }

     getAverage(*array, hours);
     printArray(size, hours);
     getMedian(*array, hours);
     getMode(*array, hours);

    cout << "STATISTICS " << endl;
    cout << "\n  Mean: " << average;
    cout << "\n  Median: " << median;
    cout << "\n  Mode: " << mode;


    return 0;
}
2
Where is nullptr supposed to be declared?guest
nullptr is a keyword in C++11M.M
neat, thanks @MattMcNabbguest
@MattMcNabb i changed it to NULL instead of nullptr is that the correct decision?user3502479
Yes, NULL is a macro that expands to 0. So that works too (so long as you're including a header that defines NULL).M.M

2 Answers

7
votes

On this line:

int average = getAverage(*array, *hours);

you refer to array and hours, however you have not declared those things yet. The "school version" of the code must have been different.

Re. the nullptr error: that was added to C++ in 2011. Perhaps the school has up-to-date compilers but you have an older compiler at home. If you change nullptr to 0 it will be fine.

1
votes

The easiest way to solve this problem is to change nullptr to 0. Though not all the time this works. But it can be a small code solution.

You can also use -std=c++11 parameter while compiling using g++. So the compiling command in the terminal will be : g++ "your file" -std=c++11