来自模板类的C ++类inhertance

时间:2018-05-09 07:51:28

标签: c++ templates inheritance visual-studio-2017

您好我是c++的新手,我的作业需要模板class 名为ShiftCipher,它的作用并不重要,它的语法完全正确并且汇编良好:

#ifndef _SHIFTCHIPHER_
#define _SHIFTCIPHER_
#pragma once
#include <iostream>
#include <string>
#define NUM_OF_CHARS 26
#define ZERO 0
#define FIRST 'a'
#define LAST 'z'
 template<int key> class ShiftCipher{
public:
std::string encrypt(std::string& str)
{
    int length = str.length();
    char* newstr = new char[length];
    for (int i = 0; i < length; i++)
    {
        if (str[i] >= FIRST && str[i] <= LAST)
        {
            if ((str[i] + key - FIRST) > NUM_OF_CHARS)
            {
                newstr = char(str[i] + key - NUM_OF_CHARS);
            }
            else
                if ((str[i] + key - FIRST) < ZERO)
                {
                    newstr = char(str[i] + key + NUM_OF_CHARS);
                }
                else
                    newstr[i] = char(str[i] + key);
        }
    }
    return newstr;
}
std::string decrypt(std::string& str)
{
    int length = str.length(),key2=-1*key;
    char* newstr = new char[length];
    for (int i = 0; i < length; i++)
    {
        if (str[i] >= FIRST && str[i] <= LAST)
        {
            if ((str[i] + key2 - FIRST) > NUM_OF_CHARS)
            {
                newstr = char(str[i] + key2 - NUM_OF_CHARS);
            }
            else
                if ((str[i] + key2 - FIRST) < ZERO)
                {
                    newstr = char(str[i] + key2 + NUM_OF_CHARS);
                }
                else
                    newstr[i] = char(str[i] + key2);
        }
    }
    return newstr;
}
};

#endif // !_SHIFTCHIPHER_

然后我有class SubstitutionChipher,它也很好用,没有特殊的东西,如模板或继承或其他任何东西。 最后一件事是OperationsRoom类,它必须来自前面提到的两个类:

#ifndef OPERATIONSROOM_H_
#define OPERATIONSROOM_H_
#define ZERO 0
#define ONE 1
#define TWO 2
#define THREE 3
#define FOUR 4
#define FIVE 5
#define SIX 6
#define MAX_MSGS_NUMBER 2
#define FIRST_SHIFT_LENGTH 5
#define SECOND_SHIFT_LENGTH 10
#define THIRD_SHIFT_LENGTH 15
#define MALICIOUSE_WORD "SpongeBob"
#include"ShiftCipher.h"
#include"SubstitutionCipher.h"
class OperationsRoom :public SubstitutionCipher, public ShiftCipher<int key>
{
 int counter, flag;
 public:
OperationsRoom() 
{
    counter = ZERO;
    flag = ZERO;
}
void getMsg(std::string& msg)
{
    std::string newmsg;
    if (counter > SIX*MAX_MSGS_NUMBER)
    {
        counter = ZERO;
        flag = ZERO;
    }
    if ((counter >= ZERO && counter < MAX_MSGS_NUMBER) || (counter >= TWO * MAX_MSGS_NUMBER && counter < THREE*MAX_MSGS_NUMBER)
        || (counter >= FOUR * MAX_MSGS_NUMBER && counter < FIVE*MAX_MSGS_NUMBER))
    {
        newmsg = SubstitutionCipher::decrypt(msg);
    }
    else
        if ((counter >= MAX_MSGS_NUMBER && counter < TWO*MAX_MSGS_NUMBER))
        {
            ShiftCipher<FIRST_SHIFT_LENGTH> shifting;
            newmsg = shifting.decrypt(msg);
        }
        else
            if ((counter >= THREE * MAX_MSGS_NUMBER && counter< FOUR*MAX_MSGS_NUMBER))
            {
                ShiftCipher<SECOND_SHIFT_LENGTH> shifting;
                newmsg = shifting.decrypt(msg);
            }
            else
                if ((counter >= FIVE * MAX_MSGS_NUMBER))
                {
                    ShiftCipher<THIRD_SHIFT_LENGTH> shifting;
                    newmsg = shifting.decrypt(msg);
                }
    int found = newmsg.find(MALICIOUSE_WORD);
    if (found != -1)
        flag = ONE;
    std::cout << "A new message has been received.\n";
    if (flag && !((counter + ONE) % MAX_MSGS_NUMBER))
    {
        flag = ZERO;
        std::cout << "Maliciouse detected!\n";
    }
    else
        if (!flag && !((counter + ONE) % MAX_MSGS_NUMBER))
        {
            flag = ZERO;
            std::cout << "Maliciouse wasnt detected!\n";
        }
 }
};
#endif

所以它一直给我错误,如:

  

错误C2146:语法错误:缺少&#39;&gt;&#39;在标识符&#39;键&#39;。

之前      

错误C2993:&#39; int&#39 ;:非类型模板参数的非法类型&#39;键&#39;

     

错误C2993:&#39; int&#39 ;:非类型模板参数的非法类型&#39;键&#39;

     

错误C2955:&#39; ShiftCipher&#39;:使用类模板需要模板参数列表

注意:请参阅ShiftCipher的声明 我在论坛上看过这些错误,但没有一个帮助我,所以我很乐意提供一些帮助。 我认为真正的问题在于使用模板来定义类OperationsRoom

1 个答案:

答案 0 :(得分:0)

您需要从ShiftCipher的特定实例继承。记住OperationRoom不是模板,因此它只需要继承特定的实例。

而不是

class OperationsRoom :public SubstitutionCipher, public ShiftCipher<int key>

你给实际的密钥..例如,如果密钥是67

class OperationsRoom :public SubstitutionCipher, public ShiftCipher<67>

如果您想为任何键概括OperationRooms,您需要为OperationRooms创建模板:类似

template<int key>
class OperationsRoom :public SubstitutionCipher, public ShiftCipher<key>