Setting up linked list class with templates?

Problem: I am trying to implement a linked list using a class template, however each of my classes that I want in the list all inherit from an Account class. I have attempted to template the linked list class as account however Ive come into errors I can't resolve. Any ideas as to how I can go about this?

The error exists within the customer.cpp class where it says

14 IntelliSense: a value of type "Account *" cannot be assigned to an entity of type "CurrentAccount *" f:\Further C++\Assignment with Templates\Assignment\Customer.cpp 22 9 Assignment


Customer class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#ifndef CUSTOMER_H
    #define CUSTOMER_H    
    #include <iostream>
    #include <string>
    #include "Account.h"
    #include "AccountLinkedList.h"
    
    using namespace std;
    
    
    class Customer
    {
    private:
    
    	string name;
    	string telNo;
    	int doorNum;
    	string street;
    	string postcode;
    	string sex;
    	string dob;
    	AccountLinkedList <Account> accountList; //Here is the linkedList templated as Account class
    	Account * head;
    	Account * aNode;
    
    public:
    
    	int customerID;
    	Customer * next;
    
    	//Customers personal details
    	Customer(int id, string customerName, string gender, int doorNumber, string customerPostcode, string dateOfBirth)
    		: customerID(id), name(customerName), sex(gender), doorNum(doorNumber), postcode(customerPostcode), dob(dateOfBirth)
    	{
    		
    	};
    	string getName();
    	void addAccount(int choice);
    	void showPersonDetails();
    	void updatePersonDetails();
    };
    
    #endif 



Here is the Customer.cpp file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include "Customer.h"
    #include "Account.h"
    #include "CurrentAccount.h"
    #include "JuniorCurrentAccount.h"
    #include "SavingsAccount.h"
    #include "CorporateSavingsAccount.h"
    #include "StudentSavingsAccount.h"
    #include <string>
    
    
    
    using namespace std;
    
    void Customer::addAccount(int choice)
    {
    	int id; // temp account ID
    	switch(choice)
    	{
         /*Current account + JuniourCurrentAccount both inherit from Account class*/
    	case 0: CurrentAccount * aNode;
    			CurrentAccount * head;
                        /*the two lines below give the error on the = sign*/
    			aNode = accountList.CreateNode(id/*newaccountID*/);
    			head = accountList.InsertFirst(head, aNode);
    			break;
    
    	case 1: JuniorCurrentAccount * aNode;
    			JuniorCurrentAccount * head;
    			aNode = accountList.CreateNode(id);
    			head = accountList.InsertFirst(head, aNode);
    	}
    }
    
    string Customer::getName()
    {
    	return name;
    }
    
    void Customer::showPersonDetails()
    {
    	cout << name << " details" << endl;
    	cout << "===============================" << endl;
    	cout << "sex: " << sex << endl;
    	cout << "dob: " << dob << endl;
    	cout << "doorNum: " << doorNum << endl;
    	cout << "postcode: " << postcode << endl;
    	cout << "===============================" << endl;
    	
    }


Here is the current Account class that inherits from account:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
 #ifndef CURRENTACCOUNT_H
    #define CURRENTACCOUNT_H
    #include <iostream>
    #include <string>
    #include "Account.h"
    
    using namespace std;
    
    class CurrentAccount : Account
    {
    private:
    		double intRate;
    		double balance;
    
    protected:
    	
    		
    public:
    	
    	CurrentAccount * next;
    
    	CurrentAccount(int accountNumber, double interestRate, double setBalance) : Account(accountNumber)
    	{
    		accountType = "Current Account";
    		intRate = interestRate;
    		balance = setBalance;
    	}
    
    	
    };
    
    #endif 
you don't do anything with templates.

What are you trying to do in addAccount()?
Declaring local variables (which hides the member variable of Customer) this way doesn't make sense.
You mention lines that give an error, yet you don't include the source for the functions you're calling on those lines. How can anyone help you? We don't know what your functions really return.
Also, you inherit privately, so no way this is gonna work. You can't access members of Account from CurrentAccount. For you to do any sort of assignment between the 2 classes or for that constructor to work, you need public inheritance, like
1
2
class CurrentAccount : public Account
{

Also, I think you've got your polymorphism the wrong way around. You want to be able to treat a child pointer (or reference, whatever) like it's a parent pointer, not the way round, e.g you want to manipulate pointer to CurrentAccount as if it were pointer to Account, so you can deal generally with Accounts. You don't want to treat an Account * as though it were a CurrentAccount *, what if it's not? A CurrentAccount is always an Account, but not the other way round.
Topic archived. No new replies allowed.