Errors C2664 and E0167, stumped

▼魔方 西西 提交于 2019-12-02 01:51:23

A string literal may be referred to by a const char*.

It may not be referred to by a char*; this was possible in old versions of C, and some older C++ compilers permitted it with a warning. In modern times it is completely prohibited.

By passing "Krister" to a function taking char*, you are asking the compiler to try to convert one to the other; it is failing, due to the above rule, as evidenced by the error message.

Chuck a const in there for great success.

If that code came from a textbook, lose it. Here is a list of good C++ books.

Just to complement the answer by Lightness Races in Orbit, which describes what you definitely should do. The reason for the difference between VS2015 and VS2017 is that the later sets the /permissive flag off by default, unlike the former.

It means that VS2017 may very well reject code that "your brother's" VS2015 accepts, unless the project options are tinkered with.

I recommend you keep the flag in its off state. Strict conformance is good, it makes you pick up better habits and write more portable C++.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!