带有继承C++的模板
Template with Inheritance C++
使用继承和模板,Ive可以按名称对员工信息数组进行排序。我有三个类,Payroll,SimpleVector和SortingVector。Payroll包含所有用户信息属性,SimpleVector创建一个Payroll类型的动态数组来存储所有用户信息。现在do SortingVector类负责按名称对数组进行排序。但每当我试图修复以前的错误时,我总是会遇到很多不同的错误。
现在发布每个类别:
工资等级
#pragma once
#include<iostream>
#include<string>
#include<vector>
using namespace std;
class Payroll {
private:
int empNumber;
string name;
double hours;
double payRate;
double grossPay;
int *aptr;
int arraySize;
public:
Payroll(int size);
Payroll();
Payroll(const Payroll & aPayroll);
~Payroll();
//Mutators
void setVector(Payroll &aPayroll);
void setEmpNumber(int empNumber);
void setName(string name);
void setHours(double hours);
void setPayRate(double payRate);
void setGrossPay(double grossPay);
//Accessors
int getEmpNumber()const;
string getName()const;
double getHours()const;
double getPayRate()const;
double getGrossPay()const;
Payroll &operator =(const Payroll &aPayroll);
bool operator ==(const Payroll &aPayroll) const;
friend ostream & operator << (ostream & output, const Payroll & aPayroll);
friend istream & operator >> (istream & input, Payroll & aPayroll);
};
//cpp
Payroll::Payroll() : empNumber(0), name(""), hours(0.00), payRate(0.00), grossPay(0.00) {}
Payroll::Payroll(const Payroll & aPayroll) : empNumber(aPayroll.empNumber), name(aPayroll.name), hours(aPayroll.hours),
payRate(aPayroll.payRate), grossPay(aPayroll.grossPay) {}
Payroll::~Payroll() {
}
//Mutators
void Payroll::setEmpNumber(int empNumber) {
this->empNumber = empNumber;
}
void Payroll::setName(string name) {
this->name = name;
}
void Payroll::setHours(double hours) {
this->hours = hours;
}
void Payroll::setPayRate(double payRate) {
this->payRate = payRate;
}
void Payroll::setGrossPay(double grossPay) {
this->grossPay = grossPay;
}
//Accessors
int Payroll::getEmpNumber()const {
return(this->empNumber);
}
string Payroll::getName()const {
return(this->name);
}
double Payroll::getHours()const {
return(this->hours);
}
double Payroll::getPayRate()const {
return(this->payRate);
}
double Payroll::getGrossPay()const {
return(this-> hours * payRate);
}
Payroll &Payroll::operator = (const Payroll &aPayroll) {
this->name = aPayroll.name;
this->empNumber = aPayroll.empNumber;
this->hours = aPayroll.hours;
this->payRate = aPayroll.payRate;
this->grossPay = aPayroll.grossPay;
return(*this);
}
bool Payroll::operator ==(const Payroll &aPayroll) const {
bool equal = this->name == aPayroll.name;
return(equal);
}
简单向量类
#ifndef SIMPLEVECTOR_H
#define SIMPLEVECTOR_H
#include <iostream>
#include <cstdlib>
#include "C:UsersJorgeDropboxPayRoll ClassPayroll.h"
using namespace std;
template <class type>
class SimpleVector {
private:
type *aptr;
int arraySize;
void subError();
public:
SimpleVector(int);
SimpleVector(const SimpleVector &aVector);
~SimpleVector();
int size() {
return arraySize;
}
type &operator [](int);
void print();
};
template <class type>
SimpleVector<type>::SimpleVector(int s) {
arraySize = s;
aptr = new type[s];
for (int count = 0; count < arraySize; count++)
aptr[count] = type();
}
template <class type>
SimpleVector<type>::SimpleVector(const SimpleVector &aVector) {
arraySize = aVector.arraySize;
aptr = new type[arraySize];
for (int count = 0; count < arraySize; count++)
aptr[count] = aVector[count];
}
template <class type>
SimpleVector<type>::~SimpleVector(){
if (arraySize > 0)
delete[] aptr;
}
template <class type>
void SimpleVector<type>::subError() {
cout << "ERROR: Subscript out of range.n";
exit(0);
}
template <class type>
type &SimpleVector<type>::operator[](int sub) {
if (sub < 0 || sub >= arraySize)
subError();
return aptr[sub];
}
template <class type>
void SimpleVector<type>::print() {
for (int k = 0; k < arraySize; k++)
cout << aptr[k] << " ";
cout << endl;
}
#endif
排序数组类
#ifndef SortingVector_h
#define SortingVector_h
#include "SimpleVector.h"
#include <iostream>
#include <string>
template <class t>
class SortingVector : public SimpleVector<t> {
public:
SortingVector(int s) : SimpleVector<t>(s) {}
SortingVector(SortingVector &aSort);
SortingVector(SimpleVector<t> &aVector) : SimpleVector<t>(aVector) {}
void sortingByName(SimpleVector<Payroll> &aVector);
};
#endif
template <class t>
SortingVector<t>::SortingVector(SortingVector &aVector) : SimpleVector<t>(aVector) {}
template <class t>
void SortingVector<t>::sortingByName(SimpleVector<Payroll> &aVector) {
bool swap;
SortingVector<Payroll> temp;
int x;
do {
swap = false;
for (int i = 0; i < aVector.arraySize; i++) {
x = strcmp(aVector[i], aVector[i + 1]);
if (x > 0) {
temp = aVector[i];
aVector[i] = aVector[i + 1];
aVector[i + 1] = temp;
swap = true;
}
}
} while (swap);
}
现在的错误是:
Error 2 error C2248: 'SimpleVector<Payroll>::arraySize' : cannot access private member declared in class 'SimpleVector<Payroll>' c:usersjorgedropboxsimplevectorsimplevectorsortingvector.h 39 1 SimpleVector
Error 1 error C2512: 'SortingVector<Payroll>' : no appropriate default constructor available c:usersjorgedropboxsimplevectorsimplevectorsortingvector.h 32 1 SimpleVector
Error 3 error C2664: 'int strcmp(const char *,const char *)' : cannot convert argument 1 from 'Payroll' to 'const char *' c:usersjorgedropboxsimplevectorsimplevectorsortingvector.h 42 1 SimpleVector
Error 4 error C2679: binary '=' : no operator found which takes a right-hand operand of type 'Payroll' (or there is no acceptable conversion) c:usersjorgedropboxsimplevectorsimplevectorsortingvector.h 45 1 SimpleVector
Error 5 error C2679: binary '=' : no operator found which takes a right-hand operand of type 'SortingVector<Payroll>' (or there is no acceptable conversion) c:usersjorgedropboxsimplevectorsimplevectorsortingvector.h 47 1 SimpleVector
SortingVector<Payroll> temp();
没有按照您的预期进行解析(令人烦恼的解析),您需要:
SortingVector<Payroll> temp;
或
SortingVector<Payroll> temp{}; /* since C++11 */
相关文章:
- 继承函数的重载解析
- 继承期间显示未知行为的子类
- 头文件-继承c++
- 为什么在保护模式下继承升级不起作用
- 通过继承类使用来自不同命名空间的运算符
- 子目录是否继承属性,例如add_definitions,include_directories和父Cmakelist.t
- 混合组合和继承的C++问题
- 继承:构造函数,初始化C++11中基类的类C数组成员
- 从类继承时,继承的类是否会通过父类重新定义继承的变量
- 公共与私人继承
- 如何创建从同一类继承的不同对象的向量
- 如何从另一个文件继承私有成员变量和公共函数
- 在模板基类中为继承类中的可选重写生成虚拟方法
- 带有继承的C++工厂
- 我应该避免多重实现继承吗
- C++继承更改成员
- 从具有默认值的部分指定模板类继承时发生SWIG错误,具有不带默认值的正向声明
- 关于C++中具有多重继承"this"指针的说明
- 尝试使用继承和模板实现CRTP.Visual Studio正在生成编译器错误
- 如何在QT Creator上将QWidget声明为继承类的对象