C++ 从“const char*”到“char”的无效转换错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8158516/
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
Invalid conversion from "const char*" to "char" error
提问by classISover
I wanted to post this because I was not really sure what issue I was having with a simple assignment statement. I am doing a homework assignment that asks me to write structs and functions in a simple program to draw out shapes of ASCII characters. Right now I am just trying to test the functions I have written, and I am trying to assign a value to the symbol element of a Circle struct just to test out the DrawShape function I wrote. When I try to assign it a * char, I get an error message saying "error: invalid conversion from 'const char*' to 'char'". I will put the whole code in, though it is very long and unfinished. Any help with this would be appreciated. The problem I am getting is right in the beginning of the main at "circle1.char = '*' "
我想发布这个是因为我不确定我在一个简单的赋值语句中遇到了什么问题。我正在做一项家庭作业,要求我在一个简单的程序中编写结构和函数来绘制 ASCII 字符的形状。现在我只是想测试我编写的函数,我试图为 Circle 结构的符号元素赋值,只是为了测试我编写的 DrawShape 函数。当我尝试为其分配一个 * char 时,我收到一条错误消息,指出“错误:从 'const char*' 到 'char' 的无效转换”。我会把整个代码放进去,虽然它很长而且没有完成。对此的任何帮助将不胜感激。我遇到的问题就在“circle1.char = '*'”的主要开头
#include <iostream>
#include <math.h>
#include <cstdlib>
using namespace std;
const int NUMBER_OF_ROWS = 26;
const int NUMBER_OF_COLUMNS = 81;
char drawSpace[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS];
struct Point{
int x;
int y;
};
struct Circle{
Point center;
int radius;
char symbol;
bool buffer[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS];
};
bool setCircleRadius(Circle &b, int r);
bool setCircleCenter(Circle &b, int x, int y);
bool moveCircle(Circle &b, int x, int y);
void drawCircle (Circle b);
void lineChars(Line a);
void circleChars(Circle b);
void drawShapes();
int main() {
Circle circle1;
circle1.radius = 5;
circle1.symbol = "*";
circle1.center.x = 40;
circle1.center.y = 10;
drawCircle(circle1);
return 0;
}
回答by Platinum Azure
You should be using single quotes for characters. Double quotes means you're using a (potentially single-character) string literal, which is represented as a const char *
(pointer to constant character).
您应该对字符使用单引号。双引号意味着您正在使用(可能是单字符)字符串文字,它表示为const char *
(指向常量字符的指针)。
Correct syntax: circle1.symbol = '*';
正确的语法: circle1.symbol = '*';
回答by Adam Trhon
The problem is here:
问题在这里:
circle1.symbol = "*";
circle1.symbol
is defined as char
, but you assign a string to it (an array of chars). What you need to do is
circle1.symbol
定义为char
,但您为其分配了一个字符串(字符数组)。你需要做的是
circle1.symbol = '*';
回答by K-ballo
Your definition of Circle
says that symbol
is a char
, yet you try to assign it a string literal of type char[2]
:
您的定义Circle
说这symbol
是 a char
,但您尝试为它分配一个类型为的字符串文字char[2]
:
circle1.symbol = "*";
Instead, you should be assigning it a char
:
相反,您应该为其分配一个char
:
circle1.symbol = '*';
回答by Jonathan Leffler
You have:
你有:
circle1.symbol = "*";
You need:
你需要:
circle1.symbol = '*';
回答by zeebonk
The "symbol" member of your Circle struct is defined as a single char. Although it looks like you are assigning a char, you are actually assigning a string or char* of length 1. The difference: char a = 'a'; char *a = "a"; It's all in the quotes.
Circle 结构的“符号”成员被定义为单个字符。虽然看起来你在分配一个字符,但实际上你在分配一个长度为 1 的字符串或 char*。区别: char a = 'a'; 字符 *a = "a"; 这一切都在引号中。
回答by celtschk
In C++, a single char is not written with double quites but with single quotes, i.e. '*'
, not "*"
. Actually "*" is an array of twochars, the first one being '*'
and the second one being '\0'
to mark the end of the string.
在 C++ 中,单个字符不是用双引号写的,而是用单引号写的,即'*'
, not "*"
。实际上“*”是两个字符的数组,第一个是'*'
,第二个是'\0'
标记字符串的结尾。
回答by mah
Your error is at the line circle1.symbol = "*";
.
"*"
is a const char *
symbol
(of your struct) is a char
Try: circle1.symbol = '*';
你的错误是在线circle1.symbol = "*";
。
"*"
是一个const char *
symbol
(你的结构)是一个char
尝试:circle1.symbol = '*';