我加载了一个加密文本文件的代码,该文本文件的第一行是一个密码,用于解码文本文件的其余部分。我知道如果函数没有在main之前定义,通常会发生这个错误,但我在头文件中定义了它,所以我不确定是什么导致了这个问题。
#include <string>
#include <iostream>
#include <fstream>
std::string decrypt(std::string cipher, std::string text);
int main()
{
//variable
std::string fileName;
std::string cipher;
std::string text;
//prompt the user to enter the file name
std::cout << "Enter file name: ";
std::cin >> fileName;
//declare input stream
std::ifstream inFile(fileName);
//Checks if the file has been connected or not
if (inFile.is_open())
{
std::cout << "File found." << std::endl;
std::cout << "Processing..." << std::endl;
getline(inFile,cipher); //get the first line as one string
std::cout << cipher << std::endl; //TEST!!!!
std::cout << "\n" << std::endl; //TEST!!!!!!
while (inFile.good()) //continue running until an error occurs to get the rest of the text as second string
{
getline(inFile,text); //get the first line as one string
std::cout << text << std::endl; //TEST!!!!
}
}
else //if the file isn't connected
{
std::cout << "File not found." << std::endl; **(ERROR C3861 HERE)**
}
inFile.close(); //close the file
std::string finalResult = decrypt(cipher,text);
std:: cout << finalResult << std::endl;
return 0;
}
//Decrypt function: Takes 2 strings by reference, changes the second string into the unencrypted text
std::string decrypt(std::string cipher, std::string text)
{
char alphabet[27] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //create a character array of the alphabet
char code[27]; //empty array
char oldText[500]; //empty array
strcpy(code,cipher.c_str()); //copies the cipher into the array
strcpy(oldText,text.c_str()); //copies the encrypted text into the array
for (int i = 0; i < 500;i++) //Loop through the entire encrypted text
{
int k = i; //creates and resets a variable for while loop
while (alphabet[k] != oldText[i])
{
k = k+1;
}
oldText[i] = alphabet[k]; //after a match is detected, swap the letters
}
std::string newText(oldText); // converts the array back into text
return newText;
}
答案 0 :(得分:1)
您的问题是decrypt
中的这一行:
std::string text(oldText); // converts the array back into text
它不会转换旧文字&#39;回到text
;相反,它声明了一个名为test
的新局部变量。新的局部变量与名为text
的参数冲突。这是导致C2082
的行。你应该写的是:
text = oldText;
用test
中的内容替换oldText
的内容虽然完全跳过return oldText
会更简单。
在编写代码时我没有C3861
,而且你在错误的行显示C2082
这一事实让我觉得你的源文件与你的源代码不同步错误。