Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Justaddwater

C++ Linked List Accessing members

This topic is 5206 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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;


}
//AND


bool 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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!