“const char *”类型的值不能分配给“char”类型的实体 C OOP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20294015/
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-27 23:01:51  来源:igfitidea点击:

a value of type "const char *" cannot be assigned to an entity of type "char" C OOP

c++oop

提问by Luke

I am creating a class to calculate a grade for a user in C++ and I am coming across a simple yet annoying problem. I know what the errors means but I don't understand how to fix it and changing to a string actually fixes the issue but this is not what I want to do.

我正在创建一个类来为 C++ 中的用户计算成绩,但我遇到了一个简单但烦人的问题。我知道错误意味着什么,但我不明白如何修复它并更改为字符串实际上可以解决问题,但这不是我想要做的。

here is the error: const char *" cannot be assigned to an entity of type "char

这是错误:const char *”不能分配给“char”类型的实体

Code

代码

    #include <string>
using namespace std;

class Gradecalc
{
public:
    Gradecalc()
    {
        mark = 0;
    }
    int getmark()
    {
        return mark;
    }
    void setmark(int inmark)
    {
        mark = inmark;

    }
    void calcgrade()
    {
        if (mark >=70)
        {
            grade = "A";      //**ERROR IS HERE**
        }

    }
    char getgrade()
    {
        return grade;
    }

private:
    int mark;
    char grade; //VARIABLE IS DECLARED HERE
};

回答by dasblinkenlight

C++ has two types of constants consisting of characters - string literals and character literals.

C++ 有两种类型的由字符组成的常量——字符串字面量和字符字面量。

  • String literals are enclosed in double quotes, and have type of const char *
  • Character literals are enclosed in single quotes, and have type char.
  • 字符串文字用双引号括起来,类型为 const char *
  • 字符文字用单引号括起来,类型为char

String literals allow multiple characters; character literals allow only one character. The two types of literals are not compatible: you need to supply a variable or a constant of a compatible type for the left side of the assignment. Since you declared gradeas a char, you need to change the code to use a character literal, like this:

字符串文字允许多个字符;字符文字只允许一个字符。这两种类型的文字不兼容:您需要为赋值的左侧提供一个兼容类型的变量或常量。由于您声明grade为 a char,因此您需要更改代码以使用字符文字,如下所示:

grade ='A';

回答by kfsone

C and C++ use double quotes to indicate "string literal", which is verydifferent from a "character literal".

C 和 C++ 使用双引号表示“字符串文字”,这与“字符文字”非常不同。

char(which is signed) is a type capable of storing a character representation in the compiler's default character set. On a modern, Western PC, that means ASCII, which is a character set that requires 7-bits, plus one for sign. So, charis generally an 8-bit value or byte.

char(有符号)是一种能够在编译器的默认字符集中存储字符表示的类型。在现代的西方 PC 上,这意味着 ASCII,这是一个需要 7 位的字符集,加上一个符号。所以,char一般是一个8位的值或字节。

A character literal is formed using single quotes, so 'A'evaluates to ASCII code 65. ('A' == 65).

字符文字是使用单引号形成的,因此'A'计算结果为 ASCII 代码 65. ( 'A' == 65)。

On the other hand, "A"causes the compiler to write char(65) + char(0) into a part of the output program and then evaluates the expression "A"to the address of that sequence; thus it evaluates to a pointer to a char sequence, but they're in the program data itself so they are not modifiable, hence const char*.

另一方面,"A"使编译器将 char(65) + char(0) 写入输出程序的一部分,然后将表达式计算"A"为该序列的地址;因此它评估为指向字符序列的指针,但它们在程序数据本身中,因此它们不可修改,因此const char*.

You want

你要

grade = 'A';

回答by Ilya Kobelevskiy

Replace

代替

grade = "A";

by

经过

grade = 'A';

You can only assign char to char, you cannot assign string to single char, and that is what you are trying to do.

您只能将字符分配给字符,不能将字符串分配给单个字符,这就是您要尝试执行的操作。

回答by Billie

Gradeis a charvariable, "A"is a const char*type.

Gradechar变量,"A"const char*类型。

You cannot assign const char*into charvarible.

您不能分配const char*charvarible。

double quote means const char*, and single qoute means char.

双引号表示const char*,单引号表示字符。

to fix that, replace:

要解决这个问题,请替换:

grade="A"

等级=“A”

with

grade='A'.

等级='A'。