C++ 禁止将 `string` 常量转换为 `char*` - Alphabets to Morse 转换程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42074199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 15:28:41  来源:igfitidea点击:

C++ forbids converting a `string` constant to `char*` - Alphabets to Morse converting program

c++

提问by Zarish

So i was working on this assignment, i need to convert normal text into Morse code. We're studying basic c++ at the moment so I'm not allowed to use the string data type or any other complex built-in functions.So I tried doing it through a char array. When i try running it,the following error shows up " ISO C++ forbids converting a string constant to 'char*' "

所以我正在做这个作业,我需要将普通文本转换为摩尔斯电码。我们目前正在学习基本的 c++,所以我不允许使用字符串数据类型或任何其他复杂的内置函数。所以我尝试通过 char 数组来做。当我尝试运行它时,出现以下错误“ISO C++ 禁止将字符串常量转换为 'char*'”

#include <iostream>
using namespace std;

int len = 0;
char string[45] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.', ',', '?', '[', '!', '(', ')', '&' };
char* morse[45] = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", "-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", ".-.-.-", "--..--", "..--..", ".----.", "-.-.--", "-..-.", "-.--.", "-.--.-", ".-..." };

void size(char* arr)
{
    for (int i = 0; arr[i] != 0; i++) {
        len++;
    }
}

int main()
{
    char str[100];
    cout << "Enter string: ";
    cin.getline(str, 100);
    size(str);
    for (int i = 0; i < len; i++) {
        for (int j = 0; j < 45; j++) {
            if (str[i] == string[j]) {
                cout << morse[j];
                break;
            }
        }
    }
    return 0;
}

回答by eerorika

You're defining an array of char*objects. You initialize those pointers with string literals. But as the error explains, converting a string literal (called constantby the compiler) to char*is not allowed.

您正在定义一个char*对象数组。你用字符串文字初始化这些指针。但是正如错误所解释的那样,不允许将字符串文字(由编译器称为常量)转换char*为。

Solution: Converting a string literal to const char*is allowed, so you could declare an array of const char*instead. You don't appear to modify the strings pointed by the array, so this shouldn't be a problem.

解决方案:const char*允许将字符串文字转换为 ,因此您可以声明一个数组const char*。您似乎没有修改数组指向的字符串,因此这应该不是问题。



PS. You've chosen to include a standard library header, and have chosen to use using namespace std;and defined an identifier with the same name as an identifier declared by the standard library (string). That will very likely be a problem for the compiler.

附注。您已选择包含标准库头文件,并选择使用using namespace std;和定义与标准库 ( string)声明的标识符同名的标识符。这很可能是编译器的问题。

Solution: Do not use using namespace std.

解决方案:不要使用using namespace std.

Workaround: Come up with another variable name than string. The trick is to know all identifiers declared by the standard library. Since this trick isn't trivial and new identifiers will be added in future versions of the standard, I recommend the solution above instead.

解决方法:想出另一个变量名而不是string. 诀窍是知道标准库声明的所有标识符。由于这个技巧不是微不足道的,并且新的标识符将在标准的未来版本中添加,我推荐上面的解决方案。

回答by clcto

The issue, as described in the error, is that "...", for example, is a string constant and you are trying to assign to a non-constant char*. Instead you should be assigning to a const char*:

如错误中所述,问题是"...",例如,是一个字符串常量,而您正试图分配给一个非常量char*。相反,您应该分配给 a const char*

const char* morse[45] = { ".-" // ...