Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ UNSORTED LIST This a C++ program related to Unsorted list as a linked struct

ID: 3876876 • Letter: C

Question

C++ UNSORTED LIST

This a C++ program related to Unsorted list as a linked structure (even though I called it sorted list). I get 5 errors in my code (I think all of them from "GetItem" function) and don't know how to fix it.

//HEADER

#include<iostream>

#include<string>

using namespace std;

// classs sortedype

struct NodeType;

class SortedType {

public:

//constructor

SortedType();

void MakeEmpty();

bool IsFull () const;

int GetLength() const;

int GetItem(int item, bool& found);

void PutItem(int item);

//void DeleteItem(int item);

void ResetList();

int GetNextItem();

~SortedType();

private:

NodeType* listData;

int length;

NodeType* currentPos;

};

///////////////////////////////////////////////////////////////////////

#include <iostream>

#include "SortedList.h"

using namespace std;

struct NodeType

{

int info;

NodeType* next;

};

SortedType::SortedType() //length starts at = 0

{

length = 0;

listData = NULL;

} //complete

bool SortedType::IsFull() const {

NodeType* location;

try

{

location = new NodeType;

delete location;

return false;

}

catch (bad_alloc exception)

{

return true;

} //cpmplete

}

int SortedType::GetLength() const { //return length

return length;

} //complete

void SortedType::MakeEmpty()

{

NodeType* tempPtr;

while (listData != NULL)

{

tempPtr = listData;

listData = listData -> next;

delete tempPtr;

}

length = 0;

} //complete

/*int SortedType::getListElement(int i) //return list

{

return list[i];

}*/

void SortedType::PutItem(int item) //

{

NodeType* location; //declare pointer to node

location = new NodeType;

location->info = item;

location->next = listData;

listData = location;

length++;

} //complete

int SortedType::GetItem(int item, bool& found)

{

bool moreToSearch;

NodeType* location;

location = listData;

found = false;

moreToSearch = (location != NULL);

while (moreToSearch && !found)

{

switch (item.ComparedTo(info[location]))

{

case 10:

case 20: location = location->next;

moreToSearch = (location != NULL);

break;

case 15: found = true;

item = location->info;

break;

}

}

return item;

} //complete

SortedType::~SortedType()

{

NodeType* tempPtr;

while (listData != NULL)

{

tempPtr = listData;

listData = listData->next;

delete tempPtr;

}

}

void SortedType::ResetList()

{

currentPos = NULL;

} //complete

int SortedType::GetNextItem()

{

if (currentPos == NULL)

currentPos = listData;

else

currentPos = currentPos->next;

return currentPos->info;

}

ERRORS:

error C2228: left of '.ComparedTo' must have class/struct/union
_sortedlistssortedlist.cpp(99): note: type is 'int'
_sortedlistssortedlist.cpp(99): error C2065: 'info': undeclared identifier
_sortedlistssortedlist.cpp(99): fatal error C1903: unable to recover from previous error(s)

Explanation / Answer

Hi,

Few errors I found in your code:

switch (item.ComparedTo(info[location]))

This statement is under a function where item is an int type variable. It is not an object of the class.

The syntax is : object-name.function-name() (function belonging to that class whose object has been created)

Also object-name -> variable-name   (variable belonging to that class whose object has been created)

I hope you meant location.comparedTo() instead or maybe you can create a new object for the class NodeType

Also, there is not a function called comparedTo() in the code yet.

I think removing these errors as per your code needs would help.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote