Hello Bopaki,
I was not able to duplicate your errors using the shell program here, but I did get others.
In "main"
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
int main()
{
linkedListForward myList;
nodeType *first{nullptr};
//myList.buildListForward(); // Original code.
first = myList.buildListForward();
myList.printList();
myList.largestElement(first);
//{cout <<"LargestElement in myList is: "<<max;}
}
|
Please add some blank lines in your code. It makes it much easier to read and see mistakes.
Line 86 calls the function, but the function returns a value that is never captured.
In this function:
28 29 30 31 32 33 34 35 36
|
nodeType *linkedListForward::buildListForward()
{
int num{};
nodeType* first{nullptr}; // <--- Never defined and caused an error.
cout<<"Enter a list of integers ending with -999.\n";
cin >> num;
//first = NULL; // <--- Not needed if initialized when defined.
|
In this function:
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
int linkedListForward::largestElement(nodeType *first)
{
//int INT_MIN{};
//int max = INT_MIN; // INT_MIN was never defined.
// <--- Or better choice. As long as first is not empty.
int max{first->info};
// <--- Or just.
//int max{};
while(first != NULL)
{
if(max < first->info)
max = first->info;
first = first->link; // <--- Could be a problem if this changes the value in "main".
}
return max;
cout<<endl<<"Largest element in myList is : "<<max <<endl; //<--- Never reached because of return statement.
}
|
Although for line 69 you should make sure "first" is not "nullptr" before you try to use it.
From C++11 on "nullptr" is the better choice over "NULL", but "NULL" will still work.
Line 82 is never reached. Not sure if you did this for testing, but it should not be in the function.
I did not get far enough to test the "printList" function, but I did notice this line
current = first;
. This did not produce any errors, but where is "first" defined or where does it come from?
Andy