代码来自c++primer plus 6 第14章c++中的代码重用 ,第552、553页。
按照书中的源码,在visual studio 2017 中编译出现如下错误;
E0144
严重性 代码 说明 文件 行 禁止显示状态
错误(活动) E0144 "const char *" 类型的值不能用于初始化 "char *" 类型的实体 c:\Users\dell\source\repos\worker0\worker0\worker0.cpp 47
出现错误的位置:
char * Singer::pv[] = {"other", "alto", "contralto",
"soprano", "bass", "baritone", "tenor"};
Singer::pv 出现的地方:
class Singer : public Worker
{
protected:
enum {other, alto, contralto, soprano,
bass, baritone, tenor};
enum {Vtypes = 7};
private:
static char *pv[Vtypes]; // string equivs of voice types
int voice;
......
我的解决办法是:强制类型转换。
char * Singer::pv[] = {(char*)"other", (char*)"alto", (char*)"contralto",
(char*)"soprano", (char*)"bass", (char*)"baritone", (char*)"tenor"};