const static int Len = 19;
char name[Len];
plorg::plorg(char * tag)
{
CI = 50;
name = tag ;
}
如上的操作将会造成报错:
||=== 构建文件: “无目标” 在 “无项目” 中 (编译器: 未知的) ===|
error: incompatible types in assignment of ‘char*’ to ‘char [19]’|
||=== 构建 失败: 1个错误, 1个警告 (0 分, 0 秒) ===|
解决方法:
这种转换在C++中是不被允许的,但是可以使用一个方法巧妙实现:
①在开头引用C的头文件string.h
#include
②使用其中功能strcpy()
修改如下
plorg::plorg(char * tag)
{
CI = 50;
//name = tag ;
strcpy(name,tag);
}
源程序参考:
https://github.com/ghostxiu/CplusplusPrimerPlus6thEditions/blob/master/Champter10/10.6.cc
及其依赖的头文件
https://github.com/ghostxiu/CplusplusPrimerPlus6thEditions/blob/master/Champter10/user_plorg.h
近期评论