-1

我刚开始学习 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(&currentByte, 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(&currentByte, 1);
            //increments the pointer
            file1.seekg(++index);
            file2.seekp(index);
            //reads the next value
            file1.read(&currentByte, 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;
}
4

4 回答 4

1
    Encryption::Encryption(char *filename1, char *filename2)
    {
        // ...
    }

由于您不打算修改filename1, filename2指向的字符,因此应将它们声明为const char *. 否则,非正式地说,编译器担心您可能会这样做,这对于字符串文字是不允许的。

此外,由于您在 class 的定义中定义了此函数Encryption,因此您无需使用Encryption::. 所以将此行更改为

    Encryption(const char *filename1, const char *filename2)
    {
        // ...
    }
于 2021-04-23T21:57:08.247 回答
1

引用什么是文字类型

文字类型是 constexpr 变量的类型,它们可以从 constexpr 函数构造、操作和返回。

本质上,这些是可以在编译时使用的实体。因为它们是constexpr,所以一方面,它们是const,因此是只读的。要解决此问题,您需要更改为

Encryption(char const* filename1, char const *filename2)

此外,您不需要限定Encryption类的构造函数的范围,Encryption::因为它是在类本身中定义的,所以只需删除它。否则你的程序将无法编译。

于 2021-04-23T21:59:08.943 回答
1

字符串文字是const char[]C++ 中的类型。从 C++11 开始,字符串文字不能分配给非常量char*指针。这样做将允许代码改变字符串文字的数据,这是未定义的行为。为了与 C 向后兼容,在 C++11 之前允许此分配,但始终不鼓励这样做。

如果要将字符串文字传递给Encryption(),则需要将其参数的类型更改为const char*(或char const *):

Encryption::Encryption(const char *filename1, const char *filename2)
{
    file1.open(filename1, ios::in | ios::out | ios::binary);
    file2.open(filename2, ios::out | ios::binary);
}

特别是因为fstream::open()无论如何这都是需要的,并且您不会以任何方式更改该参数:

void open( const char *filename,
           ios_base::openmode mode = ios_base::in|ios_base::out );
于 2021-04-24T00:40:24.433 回答
0

正如警告告诉您的那样,不允许将字符串常量转换为char*- 这不是常量。

这样做的原因是理论上您可以更改 a 的内容char*。但是字符串常量(文字)是只读的。

因此,您应该const char*在方法中使用作为参数类型。

于 2021-04-23T21:56:47.797 回答