无法从const * char 转化为 char[6]

魔w_j剑 2014-03-09 07:12:44
student_info::student_info(const char *name)

{
stu_name =name;
}
这个应该怎么改
...全文
1219 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
pwudi922 2014-03-10
  • 打赏
  • 举报
回复
上面那个写错了 student_info::student_info(const char *name) { stu_name =const_cost<char*>(name); }
pwudi922 2014-03-10
  • 打赏
  • 举报
回复
student_info::student_info(const char *name) { stu_name =const_cost(name); }
mujiok2003 2014-03-10
  • 打赏
  • 举报
回复
strncpy(stu_name, name, sizeof(stu_name)-1);
stu_name[sizeof(stu_name)-1] = '\0';
(
修巴利耶 2014-03-10
  • 打赏
  • 举报
回复
2楼顶起来
Morrisss_ 2014-03-10
  • 打赏
  • 举报
回复
引用 7 楼 richyhuang 的回复:
强制类型转换,楼主的意思应该是编译出错之类的吧
也不是强转的问题了。楼主这做法相当于用数组直接去赋值了。
Morrisss_ 2014-03-10
  • 打赏
  • 举报
回复
引用 3 楼 pwudi922 的回复:
student_info::student_info(const char *name) { stu_name =const_cost(name); }
你居然也把字符串直接赋值?!!这不是string啊。应该用strcpy
richyhuang 2014-03-10
  • 打赏
  • 举报
回复
强制类型转换,楼主的意思应该是编译出错之类的吧
wxsxiaoK 2014-03-10
  • 打赏
  • 举报
回复
引用 1 楼 derekrose 的回复:
stu_name =name; 你认为这句话做了什么
同意二楼
幻夢之葉 2014-03-10
  • 打赏
  • 举报
回复
引用 楼主 whyandwho 的回复:
student_info::student_info(const char *name) { stu_name =name; } 这个应该怎么改
你既然构造函数参数用const修饰,那你又要改,何必加const?
波波911 2014-03-10
  • 打赏
  • 举报
回复
没明白楼主的意思!这不就是给成员变量赋值吗,有什么需要改的?
图灵狗 2014-03-09
  • 打赏
  • 举报
回复
改为strcpy(stu_name, name);
derekrose 2014-03-09
  • 打赏
  • 举报
回复
stu_name =name; 你认为这句话做了什么

65,186

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧