我刚刚开始学习 C++,在编译我的代码期间出现错误:
main.cpp:59:50: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
Encryption delta("dragons.txt", "output1.txt");
我不知道这个错误意味着什么,或者如何让它工作,所以如果有人可以向我解释为什么会发生这种情况以及如何解决这个问题,我将非常感激:)
#include <iostream>
#include <fstream>
using namespace std;
class Encryption
{
fstream file1; //source file
fstream file2; //destination file
public:
Encryption::Encryption(char *filename1, char *filename2)
{
file1.open(filename1, ios::in | ios::out | ios::binary);
file2.open(filename2, ios::out | ios::binary);
}
//encrypts the file
void Encrypt(void)
{
char currentByte;
bool currentBit;
int index = 0;
//sets the pointers to the beginning of the file
file1.seekg(0, ios::beg);
file2.seekp(0, ios::beg);
//reads the first value
file1.read(¤tByte, 1);
while (file1.good())
{
//loop for four bits
for (int c = 0; c < 4; c++)
{
//finds out if the first bit is a one
currentBit = (int)((unsigned char)currentByte / 128);
//shifts the byte over
currentByte <<= 1;
//if the first bit was a one then we add it to the end
if (currentBit)
{
currentByte += 1;
}
}
//writes the character
file2.write(¤tByte, 1);
//increments the pointer
file1.seekg(++index);
file2.seekp(index);
//reads the next value
file1.read(¤tByte, 1);
}
}
//closes both of the files
void close(void)
{
file1.close();
file2.close();
}
};
int main(void)
{
cout << "Welcome to the S.A.S encryption program.";
Encryption delta("dragons.txt", "output1.txt");
delta.Encrypt();
delta.close();
Encryption gamma("output1.txt", "output2.txt");
gamma.Encrypt();
gamma.close();
return 0;
}
引用什么是文字类型 https://en.cppreference.com/w/cpp/named_req/LiteralType
文字类型是 constexpr 变量的类型,可以从 constexpr 函数构造、操作和返回它们。
本质上,这些是可以在编译时使用的实体。既然他们是constexpr https://en.cppreference.com/w/cpp/language/constexpr,所以一方面,这些是const
,因此是只读的。要解决这个问题,您需要更改为
Encryption(char const* filename1, char const *filename2)
另外,您不需要确定构造函数的范围Encryption
与 一起上课Encryption::
因为它是在类本身中定义的,所以只需删除它即可。否则你的程序将无法编译。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)