Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;模板类的类固有性_C++_Templates_Inheritance_Visual Studio 2017 - Fatal编程技术网

C++ C++;模板类的类固有性

C++ C++;模板类的类固有性,c++,templates,inheritance,visual-studio-2017,C++,Templates,Inheritance,Visual Studio 2017,您好,我是c++新手,我有一个家庭作业需要一个模板类 称为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' temp

您好,我是
c++
新手,我有一个家庭作业需要一个模板
称为
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_
所以它总是给我一些错误,比如:

错误C2146:语法错误:标识符“key”之前缺少“>”

错误C2993:“int”:非类型模板参数“key”的类型非法

错误C2993:“int”:非类型模板参数“key”的类型非法

错误C2955:“ShiftCipher”:使用类模板需要模板参数列表

注:请参见移位iPhone的声明 我在论坛上看到了这些错误,但没有一个对我有帮助,所以我很高兴能得到一些帮助。
我认为真正的问题在于使用模板定义类
OperationsRoom

您需要从ShiftChipher的特定实例继承。记住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>
模板
班级操作室:公共替换密码、公共移位密码

很抱歉,但这是一种避免神奇数字的有趣方法。
ZERO
0
一样神奇吗?
public shiftchipher
应该是
public shiftchipher
带有
SomeKey
constepr值的
public shiftchipher
。@user463035818:这允许在一个地方将
ZERO
重新定义为
42
:-)@Jarod42是的,我也这么认为。实际上,
ZERO
0
\ifndef\u SHIFTCHIPHER\u35; define\u SHIFTCHIPHER\uu
有一个输入错误的芯片和密码模板我想是吧?
template<int key>
class OperationsRoom :public SubstitutionCipher, public ShiftCipher<key>