当我调试此prg时,出现错误:erroec2440:'='无法从'const char [10]转换为'char [10]' [英] when i debug this prg, there is an error that: erroec2440: '=' cannot convert from 'const char[10] to 'char[10]'

查看:95
本文介绍了当我调试此prg时,出现错误:erroec2440:'='无法从'const char [10]转换为'char [10]'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我调试此prg时,出现错误:erroec2440:="="无法从"const char [10]转换为" char [10].我该怎么办?

when i debug this prg, there is an error that: erroec2440: ''='' cannot convert from ''const char[10] to ''char[10]''. what should i do?

class student
{public:

    char name[10];
    int code;
    float grade;



};
int main()
{
    
    char subject[5];
    char subject1[5]="srch";
    char subject2[5]="info";
    char name[20];
    student stu[3];
    stu[1].name="ali souri";
    stu[2].name="moh jdhfu";
    stu[3].name="far nfjgi";
    



    
    cout<<"HELP:"<<endl;
    cout<<"if u want search, enter srch & if u want enter information, enter info."<<endl;
    cout<<"thx."<<endl;
    gets(subject);
    
    if(strcmp(subject,subject1)==0)
    {

    cout<<"enter ur name:";
    gets(name);

    for(int i=0 ; i<3 ; i++)
    {
    if(strcmp(name,stu[i])==0)
    {
        cout<<"that's ok"<<endl;
    }
    else
    {
        cout<<"they are not match"<<endl;
        cout<<"do u want retry?";
    }
    }
    }
    else
        cout<<"bye";
    
    getchar();
    getchar();
    return 0;
}

推荐答案

这取决于您要执行的操作,但是您尝试执行的操作并不好.相反,您可以(例如)执行以下操作:
It depends on what you want to do, but what you are trying to do is not good. Instead, you could do — for example — this:
#include <cstring>

//...

    strcpy(stu[1].name, "ali souri");
    strcpy(stu[2].name, "moh jdhfu");
    strcpy(stu[3].name, "far nfjgi");


在这种情况下,您将复制字符数据,而不是指针,这在这种情况下确实很糟糕.

原因如下:赋值运算符右边的字符串是立即常量,字符串数据可以直接存储在代码中(通常以这种方式存储),并且代码是只读的,受保护的在CPU级别进行修改.尝试在该区域中写入将产生一般性保护错误(在Intel CPU中).将常量指针分配给非常量指针将打开对内存中只读区域的写访问,因此,可以说,给数组sty[1].name的任何元素分配了不同的值,这是致命的结果.

是的,C ++具有常量转换运算符(请参见 http://en.cppreference.com/w/cpp/language /const_cast [ ^ ]),但是由于我上面已经解释的严重原因,这不是您想要执行的操作.



顺便说一句,是否有特定的原因要使用以空值结尾的字符串,而不是std::string?按照C ++的精神,这将是安全,可靠,容易的,...

—SA


In this case, you would copy character data, but not the pointers, which would be really bad in this case.

Here is why: the strings on the right of assignment operator are immediate constants, the string data could be stored right in the code (and usually stored this way), and the code is read-only, protected from modification on the CPU level. An attempt to write in this area would produce a general protection fault (in Intel CPUs). An assignment of a constant pointer to a non-constant pointer would open the write access to the read-only area in the memory, so you could, say, assign a different value to any element of the array sty[1].name, with the fatal result.

Yes, C++ has the constant cast operator (please see http://en.cppreference.com/w/cpp/language/const_cast[^]), but this is not what you would want to do, by the serious reason I''ve explained above.



By the way, is there a specific reason to use null-terminated strings, not std::string? It would be safe, reliable, easy, more in the spirit of C++…

—SA


除了SA的使用std :: string的建议外,我还想添加一些内容...

如果您正在学习如何使用C ++编程,并且正在编写类似的代码,那么您的效率就不会很高.要求您退还大学学费,烧毁您正在使用的教科书,或者永远不要访问要学习的网站-这对您没有帮助.

如果您是另一种语言的程序员,请购买Kernig和Moo的"Accelerated C ++"副本.如果这是您的母语,请购买Stroustrup的编程-使用C ++的原理和实践"的副本.两者都是很好的自学教材,不会通过教给你已经养成的垃圾习惯而卖给你.

干杯,



PS:这不是解决方案,只是关于废话C ++教学的一句话
In addition to SA''s exhortation to use std::string I''d like to add a few things...

If you''re learning how to program in C++ and you''re writing code like that then you''re not doing it very effectively. Demand your course fees back off the university, burn the textbook you''re using or never go to the website you''re learning off - they''re doing you no favours.

If you''re a programmer in another language, go and buy a copy of "Accelerated C++" by Kernig and Moo. If this is your first language go and buy a copy of "Programming -- Principles and Practice Using C++" by Stroustrup. Both are excellent texts for self study and won''t sell you short by teaching you the rubbish habits you''ve picked up.

Cheers,

Ash

PS: This isn''t a solution, just a rant about crap C++ teaching


您可以初始化一个数组,例如
char subject1 [5] ="srch";
但是之后,您将不能使用此类表示法来复制字符串.
那是subject1 ="oops",会给你编译器错误

您应该使用strcpy(或使用strcpy_s获得更安全的版本)

jkchan
http://cgmath.blogspot.com [ ^ ]
you can initialize an array like
char subject1[5]="srch";
but after that you cannot use such notations for copying strings.
that is subject1 = "oops" , will give you compiler error

you should use strcpy (or strcpy_s for safer version )

jkchan
http://cgmath.blogspot.com[^]


这篇关于当我调试此prg时,出现错误:erroec2440:'='无法从'const char [10]转换为'char [10]'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆