使用模板设置链接列表类?

时间:2013-12-02 23:04:16

标签: c++ templates inheritance linked-list

我正在尝试使用类模板实现链接列表,但是我在列表中想要的每个类都继承自Account类。我试图将链表类模板化为帐户,但是我遇到了无法解决的错误。关于如何解决这个问题的任何想法?

customer.cpp类中存在错误,其中包含

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

客户类:

#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

以下是Customer.cpp文件:

#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;

}

以下是从帐户继承的当前Account类:

#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

0 个答案:

没有答案