错误:未在此范围内声明'myfile'

Crj*_*rju 2 c++ file input

好的,我正在尝试将加密文本写入文件.

由于某种原因,我得到一个未在此范围内声明编译错误.

这是我的encryption.cpp文件:

#include <iostream>
#include <fstream>
#include <string>

class encrypt{
    public:
        static void cryptText(std::string newpass)
        {
            int x = newpass.size();

            int output[x -1];

            for(int i = 0; i <= x -1; i++)
            {
                output[i] = (int)newpass[i];
                std::cout << char(output[i] + x);
                //Here I am trying to write output onto the file. For some reason, though,
                //I get the error.
                myfile.open ("passwords.thaddeus");
                myfile << output << std::endl;
                myfile.close();

            }

            std::cout << std::endl;

        }

};
Run Code Online (Sandbox Code Playgroud)

我查看了带文件的输入/输出的cplusplus.com文档.我将他们的示例程序复制到Code :: Blocks中,它运行得很好.但是当我尝试它时,我得到了错误.这很奇怪,因为我包括在内.

Adi*_*lli 7

你既没有声明也没有定义你的 myfile

因此它在当前上下文中不存在,这就是您收到此错误的原因

你错过了这部分代码 ofstream myfile;

  • 您缺少对 `std::` 的需求,幸好他的代码没有 `using namespace std;`。 (2认同)

Pol*_*ino 5

您错过了任何语言的基础知识……您的myfile定义在哪里?

  • @Syd 因为他们定义了它。`ofstream myfile;` (3认同)