这里不允许函数定义:void encryption(ifstream encrypt_file,ofstream key

a function-definition is not allowed here: void encryption(ifstream encrypt_file, ofstream keys_out, ofstream cipher_out) {

本文关键字:encrypt file key ofstream ifstream 函数 不允许 定义 encryption void 这里      更新时间:2023-10-16

我认为错误来自void函数声明和定义之间的某个地方。

在类中,我们一直在读取void函数中文件名的输入值,但对于此赋值,它们将在主类中读取。

我希望能够读入主类中的值,然后我将在encryption()和decryption(()函数中使用加密和解密方法进行编码。考虑到代码是在主类中打开的,而不是void函数,有什么关于如何做到这一点的建议吗?

错误:

在函数"int main()"中:119:79:错误:函数定义在"{"令牌无效解密(ifstreamdecrypt_in、流密钥_in和流明文_out){^125:80:错误:此处不允许在"{"之前使用函数定义令牌无效加密(ifstream encrypt_file,ofstream keys_out,流密码输出){^125:80:错误:此处不允许在"{"之前使用函数定义代币

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cctype>

using namespace std;
void decryption(ifstream& decrypt_in, ifstream& keys_in, ofstream& 
plaintxt_out);
void encryption(ifstream& encrypt_in, ofstream& keys_out, ofstream& 
cipher_out);
int main() {

enum encrypt_and_decrypt { ENCRYPT = 'E', DECRYPT = 'D'};
char enc_or_dec; 
ifstream decrypt_in;
ifstream encrypt_in;
ifstream keys_in;
ofstream plaintxt_out;
ofstream keys_out;
ofstream cipher_out;

cout << "Enter E or D: ";
cin >> enc_or_dec;
if (enc_or_dec == 'e') {
enc_or_dec = ENCRYPT;
}
if (enc_or_dec == 'd') {
enc_or_dec = DECRYPT;
}

while (enc_or_dec != ENCRYPT && enc_or_dec != DECRYPT) {
cout << "Invalid entry. Please try again. nWould you like to ENCRYPT or DECRYPT the file? ";
cin >> enc_or_dec;
}
switch (enc_or_dec) {
default:
cout << "Error";
break; 
case DECRYPT:

char decrypt_file[33];
char plaintext_file[33];
char keys_file[33];
cout << "Enter the name of your input file you want to decrypt: n";
cin >> decrypt_file;
cout << "Enter the name of the output file to write the plaintext: n";
cin >> plaintext_file;
cout << "Enter the file name that contains your encryption keys: n";
cin >> keys_file;
decrypt_in.open(decrypt_file);
if (decrypt_in.fail()) {
cout << "Input file opening for decryption failed.";
exit(EXIT_FAILURE);
}
keys_in.open(keys_file);
if (keys_in.fail()) {
cout << "Input file opening for encryption keys failed.";
exit(EXIT_FAILURE);
}
plaintxt_out.open(plaintext_file); //connect to the output file and test
if (plaintxt_out.fail()) {
cout << "Output file opening for plaintext failed. n";
exit(EXIT_FAILURE); //exit if cannot open
}
decryption(decrypt_in, keys_in, plaintxt_out);
break;
case ENCRYPT:

char encrypt_file[33];
char ciphertext_file[33];
char keys_file[33];
cout << "Enter the name of your input file you want to encrypt: n";
cin >> encrypt_file;
cout << "Enter the name of the output file to write the ciphertext: n";
cin >> ciphertext_file;
cout << "Enter the file name that will contain your encryption keys: n";
cin >> keys_file;
encrypt_in.open(encrypt_file);
if (encrypt_in.fail()) {
cout << "Input file opening for encryption failed.";
exit(EXIT_FAILURE);
}
keys_out.open(keys_file);
if (keys_out.fail()) {
cout << "Output file opening for encryption keys failed.";
exit(EXIT_FAILURE);
}
cipher_out.open(ciphertext_file); //connect to the output file and test
if (cipher_out.fail()) {
cout << "Output file opening for ciphertext failed. n";
exit(EXIT_FAILURE); //exit if cannot open
}
encryption(encrypt_in, keys_out, cipher_out); 
break;
}
void decryption(ifstream& decrypt_in, ofstream& keys_in, ofstream& plaintxt_out) {
decrypt_in.close();
keys_in.close();
plaintxt_out.close();
}
void encryption(ifstream& encrypt_file, ofstream& keys_out, ofstream& cipher_out) {
encrypt_file.close();
keys_out.close();
cipher_out.close();
}
}

返回0;

看起来函数定义实际上在main()的范围内,这是不允许的。尝试将文件末尾的悬空"}"上移几行。

你的大括号与你想象的不匹配。错误消息告诉您,您不能在定义函数的地方定义它——它实际上在main()中。在下面引用您的代码:

int main() {
...
switch(enc_or_dec) {
...
} // (1) <-- closing switch() statement
void decryption(ifstream& decrypt_in, ofstream& keys_in, ofstream& plaintxt_out) {
decrypt_in.close();
keys_in.close();
plaintxt_out.close();
}
void encryption(ifstream& encrypt_file, ofstream& keys_out, ofstream& cipher_out) {
encrypt_file.close();
keys_out.close();
cipher_out.close();
}
} // (2) <-- closing main()

您会注意到,您(可能)认为的大括号闭合main()实际上是闭合switch(enc_or_dec)。这就是为什么在文件末尾有一个随机大括号的原因。删除(2)处的大括号,并在(1)处添加另一个大括号,通过将encryptiondecryption的定义置于main之外,可以解决此问题。