【问题标题】:Error C3861: 'decrypt' not found even with function in header错误 C3861:即使在标头中有函数也找不到“解密”
【发布时间】:2014-04-19 01:38:11
【问题描述】:

我的代码加载了一个加密的文本文件,这个文本文件的第一行是一个密码,用于解码文本文件的其余部分。我知道如果函数未在 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;
}

【问题讨论】:

    标签: c++ visual-studio compiler-errors runtime-error


    【解决方案1】:

    你的问题是decrypt中的这一行:

     std::string text(oldText); // converts the array back into text
    

    它不会将“oldText”转换回text;相反,它声明了一个名为test 的新局部变量。这个新的局部变量与名为text 的参数冲突。这是导致C2082 的行。你应该写的是:

    text = oldText;
    

    这会将test 的内容替换为oldText 中的内容,尽管完全跳过它而只跳过return oldText 会更简单。

    编译您的代码时我没有收到C3861,而且您在错误的行上显示C2082 的事实让我认为您的源文件与您的错误不同步。

    【讨论】:

    • 感谢您的回复。我已经修复了 C2082,但是,我仍然收到 C3861 错误。我尝试将冷复制回一个新的 cpp 文件,但仍然出现错误。这一次,它说错误在第 45 行 "getline(inFile,text); //将第一行作为一个字符串"
    • @user3550643 std::getline
    猜你喜欢
    • 1970-01-01
    • 2012-08-18
    • 2013-12-31
    • 2013-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多