带有继承C++的模板

Template with Inheritance C++

本文关键字:C++ 继承      更新时间:2023-10-16

使用继承和模板,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 */