C++ Linked List Accessing members

Started by
3 comments, last by Justaddwater 20 years, 1 month ago
Hi all again- Ok I have a linked list that is tracking objects from 3 derived classes. It works fine, no biggie. Here is what does not work. Each class has a int itemnum and i want to be able to look up that object by that unique number. But I just cant figure how to get that information passed to my list to find the object and send it back. I will paste some code here, hopefully will make sense without haveing to paste all the lines of code.


//This is in my Linked list class


bool CList::verify(CProduct* Item)
{
Node *rec = head;
	while (rec != NULL) 
{
	if (rec->data->Compare(Item))
			return false;
		
	rec = rec->next;
			

}

//This is in my Base class


bool CProduct::Compare(CProduct *CompareItem)
{
	if (this->itemnum = itemnum) 
	return true;
	else
	return false;
}

//These are data of CProduct


protected:
	float price;
	int itemnum;
	int quantity;
	char desc[100];
	static int prodcount;

//So I want to type in a itemnum in my main, 

//and have it go find it - right now i have this 

//but problems are if I make itemnum a Int it wont 

//like going from int to *CProduct, and I can't 

//think of how I could make it a instance of a 

//class since I only want to check against what 

//is already there not make more or type in more 

//then just the itemnum


cout << "\t\t\tPlease Enter Item Number: ";
cin >> itemnum;
Save.verify(itemnum);

[edited by - justaddwater on March 17, 2004 8:49:22 PM]
Advertisement
Er, 2 bugs in one line:

if (this->itemnum = itemnum)

Firstly, this->itemnum and itemnum are the same variable. You probably meant itemnum and CompareItem->itemnum.

Secondly, you used ''='' when you should have used ''==''. That tends to break things.

If all you want is to look up an item by the number, surely the function should be:

CProduct* CList::verify(int itemNum)
not
bool CList::verify(CProduct* Item)

Just pass in the number, look at each item in the list, and return the item once you find one with the right number.

[ MSVC Fixes | STL Docs | SDL | Game AI | Sockets | C++ Faq Lite | Boost
Asking Questions | Organising code files | My stuff | Tiny XML | STLPort]
Thanks for that, bare with me, I''m still green to this all, so I now have

CProduct* CList::verify(int itemnum){Node *rec = head;	while (rec != NULL) {	if (rec->data->Compare(Item))			return false;			rec = rec->next;			}//ANDbool CProduct::Compare(CProduct *CompareItem){	if (this->itemnum == itemnum) 	return true;	else	return false;}


If I try and replace this->itemnum with compare->itemnum I get a error.. but with these changes it still does not work, I think I''m missing something in the logic of what I''m supposed to change, if you have a minute and could clarity I would be greatful.
Ok Ive made a change-

CProduct* CList::verify(int Items){	Node *rec = head;		while (rec != NULL)	{	if (rec->data->itemnum == Items)	{		cout << "MATCH FOUND";		system ("pause");		//print(rec->data);		return rec->data;	}	rec = rec->next;		system ("pause");		}	return NULL;//EDIT changed this to NULL now it 	            //does not blow up, but I still cant print                     //the item}


This seems to be able to find the item... but I cant print it out and if I enter a itemnum that does not exist it blows up

[edited by - justaddwater on March 17, 2004 10:46:00 PM]
Ignore this, I got it working finally, thank you for your help, I just needed to go in the right direction!

This topic is closed to new replies.

Advertisement