【发布时间】:2013-10-24 15:30:38
【问题描述】:
好的,我正在尝试将加密文本写入文件。
由于某种原因,我收到一个未在此范围内声明的编译错误。
这是我的 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;
}
};
我查看了 cplusplus.com 文件中的输入/输出文档。我将他们的示例程序复制到 Code::Blocks 中,它运行良好。但是当我尝试它时,我得到了错误。这很奇怪,因为我包括了。
【问题讨论】:
-
您能否将您的代码和信息减少到仍然显示此特定错误的必要最小值?您提供的大多数信息都不相关,只会影响问题。也就是说,这个错误是不言自明的。
-
除了你没有声明
myfile你在output[i]+x使用 i == 0 超出了数组访问范围 -
@Shafik Yaghmour 我添加了这一行,但得到了同样的错误,但告诉我 ofstream 没有在这个范围内声明。
-
@Syd 是的,即 b/c 样本也有
using namespace std;,这很糟糕,所以如果没有,你需要做std::ofstream myfile;。