奇怪的错误,错误:从‘const char*’到‘char’的无效转换[-fpermissive]

Posted

技术标签:

【中文标题】奇怪的错误,错误:从‘const char*’到‘char’的无效转换[-fpermissive]【英文标题】:Weird Error, error: invalid conversion from ‘const char*’ to ‘char’ [-fpermissive] 【发布时间】:2012-08-27 01:10:55 【问题描述】:

每次我尝试编译这个(第 110 行错误)

   #include <iostream>
std::string anything;
int end;
char page1[10] [10];
char Pix1;
char Pix2;
char Pix3;
char Pix4;
char Pix5;
char Pix6;
char Pix7;
char Pix8;
char Pix9;
char Pix10;
char Pix11;
char Pix12;
char Pix13;
char Pix14;
char Pix15;
char Pix16;
char Pix17;
char Pix18;
char Pix19;
char Pix20;
char Pix21;
char Pix22;
char Pix23;
char Pix24;
char Pix25;
char Pix26;
char Pix27;
char Pix28;
char Pix29;
char Pix30;
char Pix31;
char Pix32;
char Pix33;
char Pix34;
char Pix35;
char Pix36;
char Pix37;
char Pix38;
char Pix39;
char Pix40;
char Pix41;
char Pix42;
char Pix43;
char Pix44;
char Pix45;
char Pix46;
char Pix47;
char Pix48;
char Pix49;
char Pix50;
char Pix51;
char Pix52;
char Pix53;
char Pix54;
char Pix55;
char Pix56;
char Pix57;
char Pix58;
char Pix59;
char Pix60;
char Pix61;
char Pix62;
char Pix63;
char Pix64;
char Pix65;
char Pix66;
char Pix67;
char Pix68;
char Pix69;
char Pix70;
char Pix71;
char Pix72;
char Pix73;
char Pix74;
char Pix75;
char Pix76;
char Pix77;
char Pix78;
char Pix79;
char Pix80;
char Pix81;
char Pix82;
char Pix83;
char Pix84;
char Pix85;
char Pix86;
char Pix87;
char Pix88;
char Pix89;
char Pix90;
char Pix91;
char Pix92;
char Pix93;
char Pix94;
char Pix95;
char Pix96;
char Pix97;
char Pix98;
char Pix99;
char Pix100;
std::string PageNumber;
using namespace std;

int main()
//Reset level into Screen Mem
page1 [0] [0] = "T";
if(PageNumber == "page1")
     Pix1 = page1 [0] [0];
     Pix2 = page1 [0] [1];
     Pix3 = page1 [0] [2];
     Pix4 = page1 [0] [3];
     Pix5 = page1 [0] [4];
     Pix6 = page1 [0] [5];
     Pix7 = page1 [0] [6];
     Pix8 = page1 [0] [7];
     Pix9 = page1 [0] [8];
     Pix10 = page1 [0] [9];
     Pix11 = page1 [1] [0];
     Pix12 = page1 [1] [1];
     Pix13 = page1 [1] [2];
     Pix14 = page1 [1] [3];
     Pix15 = page1 [1] [4];
     Pix16 = page1 [1] [5];
     Pix17 = page1 [1] [6];
     Pix18 = page1 [1] [7];
     Pix19 = page1 [1] [8];
     Pix20 = page1 [1] [9];
     Pix21 = page1 [2] [0];
     Pix22 = page1 [2] [1];
     Pix23 = page1 [2] [2];
     Pix24 = page1 [2] [3];
     Pix25 = page1 [2] [4];
     Pix26 = page1 [2] [5];
     Pix27 = page1 [2] [6];
     Pix28 = page1 [2] [7];
     Pix29 = page1 [2] [8];
     Pix30 = page1 [2] [9];
     Pix31 = page1 [3] [0];
     Pix32 = page1 [3] [1];
     Pix33 = page1 [3] [2];
     Pix34 = page1 [3] [3];
     Pix35 = page1 [3] [4];
     Pix36 = page1 [3] [5];
     Pix37 = page1 [3] [6];
     Pix38 = page1 [3] [7];
     Pix39 = page1 [3] [8];
     Pix40 = page1 [3] [9];
     Pix41 = page1 [4] [0];
     Pix42 = page1 [4] [1];
     Pix43 = page1 [4] [2];
     Pix44 = page1 [4] [3];
     Pix45 = page1 [4] [4];
     Pix46 = page1 [4] [5];
     Pix47 = page1 [4] [6];
     Pix48 = page1 [4] [7];
     Pix49 = page1 [4] [8];
     Pix50 = page1 [4] [9];
     Pix51 = page1 [5] [0];
     Pix52 = page1 [5] [1];
     Pix53 = page1 [5] [2];
     Pix54 = page1 [5] [3];
     Pix55 = page1 [5] [4];
     Pix56 = page1 [5] [5];
     Pix57 = page1 [5] [6];
     Pix58 = page1 [5] [7];
     Pix59 = page1 [5] [8];
     Pix60 = page1 [5] [9];
     Pix61 = page1 [6] [0];
     Pix62 = page1 [6] [1];
     Pix63 = page1 [6] [2];
     Pix64 = page1 [6] [3];
     Pix65 = page1 [6] [4];
     Pix66 = page1 [6] [5];
     Pix67 = page1 [6] [6];
     Pix68 = page1 [6] [7];
     Pix69 = page1 [6] [8];
     Pix70 = page1 [6] [9];
     Pix71 = page1 [7] [0];
     Pix72 = page1 [7] [1];
     Pix73 = page1 [7] [2];
     Pix74 = page1 [7] [3];
     Pix75 = page1 [7] [4];
     Pix76 = page1 [7] [5];
     Pix77 = page1 [7] [6];
     Pix78 = page1 [7] [7];
     Pix79 = page1 [7] [8];
     Pix80 = page1 [7] [9];
     Pix81 = page1 [8] [0];
     Pix82 = page1 [8] [1];
     Pix83 = page1 [8] [2];
     Pix84 = page1 [8] [3];
     Pix85 = page1 [8] [4];
     Pix86 = page1 [8] [5];
     Pix87 = page1 [8] [6];
     Pix88 = page1 [8] [7];
     Pix89 = page1 [8] [8];
     Pix90 = page1 [8] [9];
     Pix91 = page1 [9] [0];
     Pix92 = page1 [9] [1];
     Pix93 = page1 [9] [2];
     Pix94 = page1 [9] [3];
     Pix95 = page1 [9] [4];
     Pix96 = page1 [9] [5];
     Pix97 = page1 [9] [6];
     Pix98 = page1 [9] [7];
     Pix99 = page1 [9] [8];
     Pix100 = page1 [9] [9];

//Screen Print
cout << Pix1 << Pix2 << Pix3 << Pix4 << Pix5 << Pix6 << Pix7 << Pix8 << Pix9 << Pix10 << endl;
cout << Pix11 << Pix12 << Pix13 << Pix14 << Pix15 << Pix16 << Pix17 << Pix18 << Pix19 << Pix20 << endl;
cout << Pix21 << Pix22 << Pix23 << Pix24 << Pix25 << Pix26 << Pix27 << Pix28 << Pix29 << Pix30 << endl;
cout << Pix31 << Pix32 << Pix33 << Pix34 << Pix35 << Pix36 << Pix37 << Pix38 << Pix39 << Pix40 << endl;
cout << Pix41 << Pix42 << Pix43 << Pix44 << Pix45 << Pix46 << Pix47 << Pix48 << Pix49 << Pix50 << endl;
cout << Pix51 << Pix52 << Pix53 << Pix54 << Pix55 << Pix56 << Pix57 << Pix58 << Pix59 << Pix60 << endl;
cout << Pix61 << Pix62 << Pix63 << Pix64 << Pix65 << Pix66 << Pix67 << Pix68 << Pix69 << Pix70 << endl;
cout << Pix71 << Pix72 << Pix73 << Pix74 << Pix75 << Pix76 << Pix77 << Pix78 << Pix79 << Pix80 << endl;
cout << Pix81 << Pix82 << Pix83 << Pix84 << Pix85 << Pix86 << Pix87 << Pix88 << Pix89 << Pix90 << endl;
cout << Pix91 << Pix92 << Pix93 << Pix94 << Pix95 << Pix96 << Pix97 << Pix98 << Pix99 << Pix100 << endl;
return 0;

我收到此错误 110:17:错误:从“const char*”到“char”的无效转换 [-fpermissive] 我希望二维数组(第 4 行)不是 const char, 帮助! -李罗伯特

【问题讨论】:

为什么你有100个变量而不是char Pix[100]?或者直接从 page1 打印出两个 for 循环? 首先我不知道为什么我不做 Pix[100],我希望能够控制我是打印 page1 还是 page5*。 *page5是未来数组的例子 WTF!你听说过循环数组吗? 【参考方案1】:

你已经创建了一个二维数组。

char page1[10] [10];

这意味着其中只能存储字符。 以这种方式将字符存储在其中。

page1[0] [0] = 'T';

你现在正在做的是。

char page1[0] [0] = "T" ;

无论你在双引号 "" 内写什么,它都是常量 cstring (char*)。无论你在单引号 '' 内写什么,它都是一个字符。 您可以将字符存储在 2D array 的一个索引上,而不是整个 cstring。

【讨论】:

嘿,你能帮我做一件事吗?你能告诉我为什么我的显示系统不工作吗?我将东西插入关卡内存,但它不去显示内存 @user68537:什么是“级别内存”?如果你有一个新问题,你应该问一个新问题。我们需要更多的上下文来回答它。 好的,我会问一个新问题。只是为了解释一下,我称 2D char 数组 Level 内存以及在显示内存末尾显示的所有变量 输入时引号内的内容:“your string here”是一个字符串文字,它是由双引号括起来的字符序列,可以选择以编码前缀作为前缀(u8、R 等)。没有编码前缀的字符串文字也称为窄字符串文字。窄字符串文字的类型为 array of n const char,其中 n 是字符串的大小并具有静态存储持续时间,这意味着类型为:const char[strlen("your string这里")] 衰减为 const char*,它是指向数组中第一个字符的指针。

以上是关于奇怪的错误,错误:从‘const char*’到‘char’的无效转换[-fpermissive]的主要内容,如果未能解决你的问题,请参考以下文章

`const char *` 在库 .so 文件中存储的奇怪行为

从 const char* 到迭代器错误的“未知转换” - 另一种方法

错误 C2440:“=”:无法从“const char *”转换为“char *”

从'const char *'到'char *'的无效转换[-fpermissive]; VTK-7.1.1编译错误

全局 const char * 上的错误 LNK1169

从 unsigned const char * 类型转换为 char const *