这里不允许函数定义: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) {
我认为错误来自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)
处添加另一个大括号,通过将encryption
和decryption
的定义置于main
之外,可以解决此问题。
相关文章:
- Seg Fault Issue C++ (file IO / getline)
- 应用程序崩溃并显示"symbol _ZdlPvm, version Qt_5 not defined in file libQt5Core.so.5 with link time reference"
- Android NDK clang 编译器错误在 Windows 上显示'No such file or directory'
- File.cpp.o:OpenPose 标志 CMakeFiles/.. 的多重定义/main.cpp.o:首先在这里定
- Visual Studio C++ Project File CustomBuild Task: Filter Outp
- 已解决 - C++ 的崇高文本中的"fatal error: opencv: no such file or directory"
- 可视化 如何在C++中将字符数组转换为 FILE 类型
- 如何在从文件中读取整数时使用 file.eof()?
- "No such file or directory" C++标头问题
- 使用 sftp_open() 使用 C++ 中的 SFTP libssh 将文件从本地复制到远程时,File 返回 N
- Visual Studio "fatal error LNK1104: cannot open file"未使用或包含的库
- typedef vector size_type in header file
- make 命令创建 .file,但不创建应用程序文件
- 为什么 VScode 在 .h 文件中显示"'iostream' file not found"?
- 我在 #include < bits/stdc++h.> 放置时出错"No such file or directory"
- 使用 编译时。RES File with MinGW G++, .无法识别 RES
- fopen 在创建大量文件后因 EFBIG "File too large" 而失败
- 叮叮当当。target_include_directories "No such file or directory"
- File to std::string_view
- VS 2017 C++ - "cannot open source file 'sqlite3.h' "