C++ 使用模板设置链表类?
我试图使用类模板实现一个链表,但是我希望列表中的每个类都继承自一个Account类。我试图将链表类模板化为account,但遇到了无法解决的错误。我该怎么做呢 错误存在于customer.cpp类中C++ 使用模板设置链表类?,c++,templates,inheritance,linked-list,C++,Templates,Inheritance,Linked List,我试图使用类模板实现一个链表,但是我希望列表中的每个类都继承自一个Account类。我试图将链表类模板化为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\A
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
\ifndef客户
#定义客户
#包括
#包括
#包括“Account.h”
#包括“AccountLinkedList.h”
使用名称空间std;
类客户
{
私人:
字符串名;
字符串telNo;
int doorNum;
弦街;;
字符串邮政编码;
弦性;
字符串dob;
AccountLinkedList accountList;//以下是模板化为Account类的linkedList
账户*主管;
账户*阳极;
公众:
国际客户ID;
客户*next;
//客户个人资料
客户(整数id、字符串customerName、字符串性别、整数门牌号、字符串customerPostcode、字符串dateOfBirth)
:customerID(id)、姓名(customerName)、性别(性别)、门号(doorNumber)、邮政编码(customerPostcode)、出生日期(dob)
{
};
字符串getName();
作废账户(整数选择);
void showPersonDetails();
void updatePersonDetails();
};
#恩迪夫
以下是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;
}
#包括“Customer.h”
#包括“Account.h”
#包括“CurrentAccount.h”
#包括“JuniorCurrentAccount.h”
#包括“SavingsAccount.h”
#包括“CorporateSavingsAccount.h”
#包括“StudentSavingsAccount.h”
#包括
使用名称空间std;
无效客户::添加帐户(整数选择)
{
int id;//临时帐户id
开关(选择)
{
/*Current account+JUnitourCurrentAccount都继承自account类*/
案例0:CurrentAccount*阳极;
经常账户*总目;
/*下面的两行给出了=符号上的错误*/
阳极=accountList.CreateNode(id/*newaccountID*/);
head=accountList.InsertFirst(head,阳极);
打破
案例1:JuniorCurrentAccount*阳极;
JuniorCurrentAccount*负责人;
阳极=accountList.CreateNode(id);
head=accountList.InsertFirst(head,阳极);
}
}
字符串Customer::getName()
{
返回名称;
}
作废客户::showPersonDetails()
{
如果没有所有的代码,我就说不出问题出在哪里。但是,您是否知道std::list
和(可能更恰当地说)std::vector
。我知道这些事情,但是出于教育的原因,我正试图教自己如何处理这些:)-Ive currentAccount类代码来帮助accountList.CreateNode()
返回一个帐户*
,但您分配给它的指针是一个更具体的类型CustomerAccount*
。我不确定您为什么这样做,但这似乎是错误的原因。@TimoUnmakin恕我直言,您肯定不会像这样处理它们,因为它们有指向头部的原始指针。如果您想编写自己的链接ed list使用定义良好的接口和RAII,尝试并复制std::list
的方式。
#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