cannot convert from 'const char [3]' to 'char'

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了cannot convert from 'const char [3]' to 'char'相关的知识,希望对你有一定的参考价值。

主要代码:
int w, h;
char *line, *lp;
(void) puts (line);
for (lp = line, w = 0; w < width; w++)
lp=(char *) malloc(10*sizeof(char));
*lp++ = "■";
if ((maze + w)->down)
*lp++ = ((maze + w)->path && (h == height - 1 ||
(maze + w + width)->path)) ? '.' : ' ';
else
lp=(char *) malloc(10*sizeof(char));
*lp++ = "■";

原来是"*lp++ = '#';没有“lp=(char *) malloc(10*sizeof(char));”,我的目的是让 '#'变为"■",因为单引号是字符,双引号是字符串,我需要输出"■",所以就添加了“lp=(char *) malloc(10*sizeof(char));”,我知道我错了,但是怎样实现我想要的功能呢?高手帮忙!
回复2楼和3楼,我在编译一个迷宫程序,你们高诉的我都编译成功了,但是2楼的程序当运行显示迷宫的时候跳出错误窗口,3楼的程序还好一点,只是显示迷宫一片空白什么都没有,这是整个函数,谢谢你们再看一下吧。我把函数放到这里了:
http://tieba.baidu.com/f?kz=514502383

回二楼friday_collin,*lp++ = 'A'并不是“表达式做左值”。
这样完全可以的:
main()

int a[10]=0,*p=a;
*p++=2;


但这样不行:
main()

int p=0;
p++=2;


cannot convert from 'const char [3]' to 'char'
这个错误的关键,是不能把字符串"■"转换成字符!

void PrintMaze (maze_t maze, int width, int height)

int w, h;
char *line, *lp;

line = (char *) calloc ((width + 1) * 2, sizeof (char));
if (line == NULL)
(void) fprintf (stderr, "Cannot allocate memory!\n");
exit (EXIT_FAILURE);

maze->up = TRUE;
(maze + (width * height) - 1)->down = TRUE;

for (lp = line, w = 0; w < width; w++)
lp=(char *) malloc(10*sizeof(char)); /////这里申请了内存
strcpy(lp,"■"); ///////////////////////这里进行了赋值
if ((maze + w)->up)
*lp++ = ((maze + w)->path) ? '.' : ' '; //////再赋值,回把刚才的赋值覆盖掉,应该用strcat追加
else
lp=(char *) malloc(10*sizeof(char)); /////这里怎么又申请了?
strcpy(lp,"■"); /////////应该用strcat追加

lp=(char *) malloc(10*sizeof(char));
strcpy(lp,"■");
(void) puts (line);
for (h = 0; h < height; h++)
for (lp = line, w = 0; w < width; w++)
if ((maze + w)->left)
*lp++ = ((maze + w)->path && (maze + w - 1)->path) ? '.' : ' ';
else
lp=(char *) malloc(10*sizeof(char));
strcpy(lp,"■");
*lp++ = ((maze + w)->path) ? '.' : ' ';

lp=(char *) malloc(10*sizeof(char));
strcpy(lp,"■");
(void) puts (line);
for (lp = line, w = 0; w < width; w++)
lp=(char *) malloc(10*sizeof(char));
strcpy(lp,"■");
if ((maze + w)->down)
*lp++ = ((maze + w)->path && (h == height - 1 ||
(maze + w + width)->path)) ? '.' : ' ';
else
lp=(char *) malloc(10*sizeof(char));
strcpy(lp,"■");

lp=(char *) malloc(10*sizeof(char));
strcpy(lp,"■");
(void) puts (line);
maze += width;

free (line);

/* PrintMaze */
参考技术A 非常感谢楼下的指点,呵呵 我原来以为表达式不可以做左值,虽然这句话是正确的,但是指针的情况特殊哦,呵呵 受教了,谢谢
我看别人做迷宫的时候的方块都是调用系统函数画二维条形图,我这共有个迷宫的程序,你有兴趣看看(学术交流,如果你的问题解决了不妨给我发个消息我也学习下,呵呵)(这个是在TC下做的):
#include"graphics.h"
#include"dos.h"
#include"conio.h"
#include"stdlib.h"
main()

int gdriver,gmode;
gdriver=DETECT;
detectgraph(&gdriver,&gmode);
registerbgidriver(gdriver);
initgraph(&gdriver,&gmode,"c:\\tc");
mainpicture();
muse();
migong();
getch();

/*==========================mainpicture()===================================*/
mainpicture()

char *title="=CYH-ZHL=";
char *love="LOVE AT FIRST SIGHT";
int i;
setfillstyle(1,8);
bar(50,10,580,450);
setfillstyle(7,2);
bar(55,15,575,445);
setfillstyle(1,3);
bar(60,20,570,440);
setfillstyle(1,0);
bar(90,50,540,410);
settextstyle(0,0,3);
setcolor(8);
for(i=0;i<5;i++)
outtextxy(200+i,25+i,title);
setcolor(4);
outtextxy(200,25,title);
setcolor(0);
settextstyle(0,1,2);
outtextxy(85,70,love);
outtextxy(565,70,love);

/*=================================muse()===================================*/
muse()

setfillstyle(1,15);
bar(110,60,520,90);
setfillstyle(1,8);
bar(112,62,518,88);
setfillstyle(1,3);
bar(120,68,180,82);
settextstyle(0,0,1);
setcolor(15);
outtextxy(127,72,"MiGong");

/*===================================migong()====================================*/
migong()

int x,y,z;
char a;
int key;
int px=1,py=1;
int abc[20][40]=1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,
1,1,1,0,1,0,1,0,1,1,1,1,1,1,1,1,1,0,1,0,1,0,1,0,1,1,1,1,1,1,1,0,1,1,0,1,1,1,0,1,
1,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,1,0,1,0,1,0,0,0,0,0,0,0,1,0,0,1,0,0,0,1,
1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,0,0,0,1,0,1,1,1,1,1,1,1,0,0,1,0,1,1,1,
1,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,1,1,1,0,1,0,0,0,1,0,1,0,1,1,0,0,0,1,
1,1,1,1,0,1,1,1,1,0,1,1,1,1,1,0,1,0,1,0,1,0,0,0,1,0,0,0,1,0,1,0,1,0,0,1,1,1,0,1,
1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,1,0,1,1,1,0,1,1,1,1,1,0,1,0,1,1,0,1,0,0,0,1,
1,0,1,1,1,1,0,1,1,0,1,1,1,1,1,1,1,0,1,0,0,0,0,0,1,0,0,0,1,0,0,0,1,0,0,1,0,1,0,1,
1,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,1,0,1,0,1,1,1,0,1,0,1,0,1,0,1,0,1,1,0,1,1,1,
1,1,1,1,1,1,1,1,0,1,1,0,1,1,1,1,1,1,1,0,1,0,1,0,0,0,1,0,1,0,1,0,1,0,0,1,0,0,0,1,
1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,1,1,0,1,0,0,0,1,0,0,1,0,1,0,0,0,1,
1,0,1,0,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,0,1,0,1,1,1,1,1,1,0,0,0,1,0,1,1,1,
1,0,1,0,1,0,0,0,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,1,0,1,1,1,0,1,0,1,
1,0,1,0,1,0,1,1,1,1,0,1,0,1,0,1,0,1,0,1,0,1,1,0,1,1,1,1,1,1,0,1,0,0,0,0,0,0,0,1,
1,0,1,0,0,0,1,0,0,0,0,1,0,1,0,1,0,1,0,1,0,1,0,0,1,0,0,0,0,0,0,1,1,1,0,1,1,1,1,1,
1,0,1,0,1,1,1,1,1,1,1,1,0,1,0,1,0,1,0,1,0,1,0,1,1,0,1,1,0,1,0,0,0,1,0,0,0,0,0,1,
1,0,1,1,1,0,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,0,0,1,1,1,0,1,0,1,1,1,1,1,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1;
for(x=0;x<20;x++)

for(y=0;y<40;y++)

setfillstyle(1,0);
if(abc[x][y]==1) bar(115+10*y,100+10*x,115+10*y+10,100+10*x+10);
settextstyle(0,0,1);
setcolor(15);
outtextxy(480,208,"EXIT");


rectangle(110,95,520,400);
rectangle(118,105,512,300);
rectangle(118,310,512,390);
outtextxy(130,315,"Esc Exit");
outtextxy(130,325,"^ Up");
outtextxy(130,335,"v Down");
outtextxy(130,345,"< Left");
outtextxy(130,355,"> Right");
setfillstyle(1,12);
bar3d(126,111,134,119,5,1);
for(;;)

key=bioskey(0);
if(key==0x11b) break;
if(key==0x4d00)

if(py+1<40 && abc[px][py+1]!=1)

setfillstyle(1,10);
bar3d(115+10*py+1,100+10*px+1,115+10*py+10-1,100+10*px+10-1,5,1);
py=py+1;
setfillstyle(1,12);
bar3d(115+10*py+1,100+10*px+1,115+10*py+10-1,100+10*px+10-1,5,1);
if(px==11 && py==36) migongimage();


if(key==0x4b00)

if(py-1>-1 && abc[px][py-1]!=1)

setfillstyle(1,10);
bar3d(115+10*py+1,100+10*px+1,115+10*py+10-1,100+10*px+10-1,5,1);
py=py-1;
setfillstyle(1,12);
bar3d(115+10*py+1,100+10*px+1,115+10*py+10-1,100+10*px+10-1,5,1);
if(px==11 && py==36) migongimage();


if(key==0x5000)

if(px+1<20 && abc[px+1][py]!=1)

setfillstyle(1,10);
bar3d(115+10*py+1,100+10*px+1,115+10*py+10-1,100+10*px+10-1,5,1);
px=px+1;
setfillstyle(1,12);
bar3d(115+10*py+1,100+10*px+1,115+10*py+10-1,100+10*px+10-1,5,1);
if(px==11 && py==36) migongimage();


if(key==0x4800)

if(px-1>-1 && abc[px-1][py]!=1)

setfillstyle(1,10);
bar3d(115+10*py+1,100+10*px+1,115+10*py+10-1,100+10*px+10-1,5,1);
px=px-1;
setfillstyle(1,12);
bar3d(115+10*py+1,100+10*px+1,115+10*py+10-1,100+10*px+10-1,5,1);
if(px==11 && py==36) migongimage();




/*========================================migongimage()===========================*/
migongimage()
setcolor(10);
settextstyle(0,0,3);
delay(2000);
rectangle(252,332,283,368);
delay(2000);
outtextxy(257,340,"G");
line(283,332,313,368);
line(283,368,313,332);
delay(2000);
rectangle(313,332,344,368);
delay(2000);
outtextxy(318,340,"O");
line(344,332,374,368);
line(344,368,374,332);
delay(2000);
rectangle(374,332,405,368);
delay(2000);
outtextxy(379,340,"O");
line(405,332,435,368);
line(405,368,435,332);
delay(2000);
rectangle(435,332,466,368);
outtextxy(440,340,"D");

参考技术B 首先,#include <string.h>

将下面这一句:
*lp++ = "■";

改为:
strcpy(lp,"■");

一点建议:
对于lp增加一下malloc是否成功的容错处理

==============================================
补充答复:
原来是做迷宫啊,迷宫的方块不是直接用"■"来堆建的,
对于编译器来说,"■"就是一个3字节的char数据,可不是我们肉眼看到的小方块。

迷宫的方块是通过以下方法画的:
相关头文件:#include<graphics.h>
涉及接口:
setfillstyle(int pattern, int color); //设置填充图样和颜色函数

例如:
setfillstyle(SOLID_FILL,RED);
参考技术C 不能把这个和那个对调

tensorflow2.报错与解决 Cannot convert a symbolic Tensor

问题

TensorFlow2.4.1(Macbook m1 conda-forge)报错:

NotImplementedError: Cannot convert a symbolic Tensor
(sequential/simple_rnn/strided_slice:0) to a numpy array. This error
may indicate that you’re trying to pass a Tensor to a NumPy call,
which is not supported

解决

numpy版本过高,conda安装指定版本的numpy,1.19.5即可解决问题

conda install numpy=1.19.5 -c conda-forge

以上是关于cannot convert from 'const char [3]' to 'char'的主要内容,如果未能解决你的问题,请参考以下文章

cannot convert parameter 1 from 'const char [] ' to 'const wchar_t *

OpenGL学习笔记-3:编译shader报错: cannot convert from 'const highp float' to 'FragUserData 4-com

C++求助[Error]cannot convert 'const wchar_t*' to 'LPCSTR aka const char*

MQ Cannot convert from [[B] to [] for GenericMessage

FFmpeg 'scale' filter not present, cannot convert pixel formats.

写入excel数据报错:ValueError: Cannot convert {'code': 0, 'msg': 'login success!',