无法引用默认构造函数错误(Visual Studio 2019)



当我试图引用我修改过的子类时,我在代码底部遇到了一个错误,如下所示:

不能引用"Revisedcipher"的默认构造函数--它是删除的功能

我不完全确定这意味着什么,也不知道如何解决。如有任何帮助,我们将不胜感激。

我的代码:

#include <iostream>
#include <string>
using namespace std;
class SubstitutionCipher {
public:
//Define your key which is a permutation of 26 alphabets
string key;
//Constructor to create an instance which initializes the key with the given permutation
SubstitutionCipher(string k) {
key = k;
}
//function to encode a string
string encode(string data) {
//initialize encoded data
string encodedData = data;
//replace each character in encodedData with the corresponding mapped data in key. We subtract a char with 'A' to get its position
for (int i = 0; i < data.length(); i++) {
encodedData[i] = key.at(data.at(i) - 'A');
}
//return the encoded data
return encodedData;
}
//function to decode a string
string decode(string data) {
//initialize decoded data
string decodedData = data;
//replace each character in decodedData with the corresponding reverse mapped data in key. We add the position with 'A' to get the corresponding char
for (int i = 0; i < data.length(); i++) {
decodedData[i] = key.find(data.at(i)) + 'A';
}
//return the decoded data
return decodedData;
}
};
class Revisedcipher : public SubstitutionCipher
{
public:
void encrypt(string text, int s)
{
string result = "";
// traverse text
for (int i = 0; i < text.length(); i++)
{
// apply transformation to each character
// Encrypt Uppercase letters
if (isupper(text[i]))
result += char(int(text[i] + s - 65) % 26 + 65);
// Encrypt Lowercase letters
else
result += char(int(text[i] + s - 97) % 26 + 97);
}
// Return the resulting string
//   return result;
cout << "n C_Cipher: " << result;
}
};
//main() method to test the implementation
int main() {
SubstitutionCipher cipher("ZYXWVUTSRQPONMLKJIHGFEDCBA");
string data = "HJOIEBSOMN";
string encodedData = cipher.encode(data);
cout << data << " encoded as " << encodedData << endl;
string decodedData = cipher.decode(encodedData);
cout << encodedData << " decoded as " << decodedData;
Revisedcipher cc;
cc.encrypt(data, 4);
}

由于您为基类SubstitutionCipher提供了一个非默认构造函数(一个带参数的构造函数(,但没有默认构造函数(即一个不带参数的构造器(,后者被隐式删除。此外,作为派生类,Revisedcipher不提供构造函数,也不覆盖已删除的基类构造函数。

要解决此问题,您需要为基类定义一个默认构造函数,为派生类提供一个构造函数(或两者都提供(。您也可以使用参数的默认值来执行此操作。

以下是如何为派生类执行此操作:

class Revisedcipher : public SubstitutionCipher {
public:
// Call the base class constructor with a given string, or the 'default' if none...
Revisedcipher(string rk = "[default]") : SubstitutionCipher(rk) {}
void encrypt(string text, int s)
{
//... and so forth

或者,您可以为基本类构造函数提供默认的std::string参数:

SubstitutionCipher(string k = "[default value]") {
key = k;
}

您的基类型SubstitutionCipher有一个用户定义的构造函数,它将禁用编译器自动生成默认构造函数。

此行:

SubstitutionCipher(string k) {
key = k;
}

任何继承它的类型都必须定义一个构造函数,该构造函数调用具有某个值的SubstitutionCipher(string)

我可以想出三种方法:

  1. 您可以通过以下行指示编译器生成默认构造函数:

    SubstitutionCipher() = default;
    

    但我认为这不是一个好主意,因为你要么必须硬编码密钥,要么将其留空。两者都不是一个好的选择。

  2. 您可以向Revisedcipher添加一个构造函数,该构造函数接受一个键参数并将其传递给基类,如下所示:

    Revisedcipher(const string& key) : SubstitutionCipher(key) { }
    
  3. 您可以考虑在Revisedcipher中添加一个默认构造函数,它将一些硬编码的键传递给基。

    Revisedcipher() : SubstitutionCipher("your hard-coded key") { }
    

p.S.:还有第四种方法。可以为基类中的key参数指定默认值。

附言:解决这个问题的第五种方法是从一些外部源获取密钥,而不是作为构造函数参数。

最新更新