Hi, I have created a generic linked list in which different objects can be added to a list as long as they inherit from my object class. My problem is this:
I am able to add nodes and find nodes etc. but I can't figure out how to return an instance of an Object which has been added.
Below shows the code I am having trouble with (although there are many different instances of this problem, they can hopefully be resolved through this one instance being solved):

int Bank::customerSearch(unsigned int custNum)
	int i;
	for(i = 0; i < theCustomers.size(); ++i)
		Customer c = theCustomers.at(i); //here is my problem
		if(c.getCustNum() == custNum)
			return i;
	return -1;

When I try to assign the customer instance my program breaks because in my list the at() function is currently returning an Object reference as opposed to a Customer reference. I need to somehow return a reference to a Customer (or Account or Mortgage) instead so that it works similar to the at() function in a vector.

I have attached the header file and cpp file that I use for my linked list.


I think you should do this:


Node* List::at(int index)
	Node *temp = head;
	int i;
	for(i = 0; i < index; ++i)
		temp = temp->next;

	return temp;

Node* at(int index);

Unfortunately that gives a similar error to the one I am currently getting

error C2440: 'initializing' : cannot convert from 'Node *' to 'Customer'

I need to somehow return an instance or pointer to customer object that is associated to its position in my list

You might want to look into Run-Time Type Identification (RTTI). I got some pretty good hits searching RTTI on Google. A good reference book is hard to beat though.

You might be able to get by with embedding the type of the object within the object so that you can use polymorphism to identify the type of the object, if all you want is to know the type.

Alternatively, you could redesign the program. Since you posted here I suspect that is your least favorite option, however.

Ok I've restructured so that I'm now using a template instead of inheriting from an object class associated with my list.

I'm almost stuck at the same point though....It is compiling and running now but when I try to print information about a customer object nothing is displayed. No errors are being thrown and I'm pretty sure the customer object is being added correctly. Can anyone explain this to me? perhaps I'm not updating the customer details correctly or my pointers aren't quite right.

Here's a test file I made to test my add functionality:

#include "Customer.h"
#include "Bank.h"

int main()
	int i;
	Bank theBank;

	//Customer information
			string name;
			string address;
			Date *dob;
			int dd;
			int mm;
			int yyyy;
			string telNo;
			char sex;

			cout << "Please enter customers name: " << endl;
			cin >> name;
			cout << "Please enter customers address: " << endl;
			cin >> address;
			cout << "Please enter customers telephone number: " << endl;
			cin >> telNo;
			cout << "Please enter customers date of birth: " << endl;/*Validate this section*/
			cout << "Day (dd): " << endl;
			cin >> dd;
			cout << "Month (mm): " << endl;
			cin >> mm;
			cout << "Year(yyyy): " << endl;
			cin >> yyyy;
			cout << "Please enter customers sex: " << endl;
			cin >> sex;

			dob = new Date(dd, mm, yyyy);

	Customer *c = new Customer();


	cin >> i;

	return 0;

I've added the new list files with the new template structure.
Any help would be greatly appreciated.

You seem to capture the customer details correctly from the user but when you create a new Customer()

Customer *c = new Customer();

how are you passing the customer details to the customer object i.e customer name, address, date of birth etc...?

Customer *c = new Customer(name, address, dob, etc.....);

Definition of template member funtions needs to be in the header file, not in the cpp file.

Where is Bank.h? From what is posted I can only guess at what's in Bank.

I hope Bank.h includes the STL string header file and the files necessary for the Customer and Date classes. Otherwise to need to include them as well.

Here's how I would restructure the Node class

template<class Type>
class Node
     Type dataItem;
     Node * next;
     Node(Type nodeData)
         dataItem = nodeData;
         next = 0;

Ok I restructured my Nodes as suggested but it made no difference other than I have to use a . and not a -> when accessing the customer functionality.
I didn't have the STL string header but even after I included it it made no difference.
I just can't see why it wouldn't be printing anything but not throwing an error and continuing to run as if it had printed.

I've attached the bank header now aswell :)


After You capture the customer details from the user, how are you passing the variables: name, address, dob, telNo and sex to the new customer?

Consider, after you capture the customer details you then create a customer c = new customer(); yes?
The customer class should have defined in it variables name, address, dob, telNo and sex. E.g:

class Customer
String name, address, telNo, sex;
Date dob;
Customer(String &n, string &a, string &t, string &s, Date &d): name(n), address(a), telNo(t), sex(s), dob(d){}

///then you various methods proceed........

Then after capturing the customer details from the user in the main method you then pass them to the new customer through the constructor to read something like

Customer *c = new Customer(name, address, dob, telNo, sex)

then when you call c->getname it should show you the name