warning: deprecated conversion from string constant to 'char*

#include<iostream>
using namespace std;
class Student
{
private:
int age;
char*name;
public:
Student(int m, char *n)
{
age=m;name=n;
}
Student()
{
age=;name="unnamed";
}
~ Student(){}
void SetMember ( int m,char *n )
{
age=m;name=n;
}
int Getage(){return age;}
char *Getname(){return name;}
};
int main()
{
Student stu[]={Student(,"wang"),Student(),Student()} ; stu[].SetMember(,"zhang"); cout<<stu[].Getage()<<","<<stu[].Getname()<<endl;
cout<<stu[].Getage()<<","<<stu[].Getname()<<endl;
cout<<stu[].Getage()<<","<<stu[].Getname()<<endl;
return ;
}

 

 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include<iostream>
using namespace std;
class Student {
private:
    int age;
    const char*name;
public:
    Student(int m, const char *n) {
        age=m;
        name=n;
    }
    Student() {
        age=0;
        name="unnamed";
    }
    ~ Student() {}
    void SetMember ( int m,const char *n ) {
        age=m;
        name=n;
    }
    int Getage() {
        return age;
    }
    const char *Getname() {
        return name;
    }
};
int main() {
    Student stu[3]= {Student(13,"wang"),Student(),Student()} ;
 
    stu[2].SetMember(12,"zhang");
 
    cout<<stu[0].Getage()<<","<<stu[0].Getname()<<endl;
    cout<<stu[1].Getage()<<","<<stu[1].Getname()<<endl;
    cout<<stu[2].Getage()<<","<<stu[2].Getname()<<endl;
    return 0;
}

看你的实现,传给Student类的字符串都是不可变的,都加上const就好了;否则你就要复制一份并且自己管理那块内存了。

warning: deprecated conversion from string constant to 'char*的更多相关文章

  1. deprecated conversion from string constant to ‘char*’

    deprecated conversion from string constant to ‘char*’ #include <iostream> using namespace std; ...

  2. warning:deprecated conversion from string constant to 'char *' 解决方案

    #include <iostream> using namespace std; int fuc(char *a) { cout << a << endl; } i ...

  3. warning:deprecated conversion from string constant to &#39;char *&#39;

    warning:deprecated conversion from string constant to 'char *' 解决方式 #include <iostream> using ...

  4. warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]

    在C++中, char* p = "abc"; // valid in C, invalid in C++ 会跳出警告:warning: ISO C++ forbids conve ...

  5. warning: ISO C++ forbids converting a string constant to 'char*'

    第1种字符串赋值方式: char * fileName="./2017-09-02-10-34-10.xml";//这一种字符串赋值方式已经被ISO禁止了 第2种字符串赋值方式: ...

  6. ISO c++11 does not allow conversion from string literal to 'char*'

    http://stackoverflow.com/questions/9650058/deprecated-conversion-from-string-literal-to-char

  7. 将string转换成char*

    string 是c++标准库里面其中一个,封装了对字符串的操作  把string转换为char* 有3中方法:  1.data  如:  string str="abc";  ch ...

  8. expected declaration specifiers or '...' before string constant

    /work/platform_bus_dev_drv/led_dev.c:52: error: expected declaration specifiers or '...' before stri ...

  9. Constant Pool和String Constant Pool详解

    Constant Pool常量池的概念: 在讲到String的一些特殊情况时,总会提到String Pool或者Constant Pool,但是我想很多人都不太明白Constant Pool到底是个怎 ...

随机推荐

  1. 新安装一个eclipse,导入一个web项目,右键点击项目选择Properties,找不到project facets和Server选项。

    解决方式: 1.点击:eclipse导航栏中点击Help->Install New Software 2.点击Add添加 3在弹出框中填写以下信息 name:keep(名字随便取) locati ...

  2. 转载别人的ftp,觉得目录结构不错,学习

    开发简单的FTP:1. 用户登陆2. 上传/下载文件3. 不同用户家目录不同4. 查看当前目录下文件5. 充分使用面向对象知识 REDMAE 1 用户登陆 2 3 1.查看用户目录文件 4 2.上传文 ...

  3. html 按钮跳转问题(及其相关)

    1.点击一个按钮跳转到另一个页面 (网址)   两种写法: <button onclick="{location.href='location.html'}">获取现在 ...

  4. iOS之CAScrollLayer属性简介和使用

    1.CAScrollLayer的简介 CAScrollLayer用于显示一个滑动图层的一部分,可以确定滑动方向和可视区域面积,限制不滑出区域外!相关属性如下:其中 /* Scroll the cont ...

  5. 按钮事件v-on

    <!DOCTYPE html> <html lang="zh"> <head> <title></title> < ...

  6. 密码学笔记(2)——RSA密码

    上一篇笔记中讲述了大量的代数知识,这一篇中我们看看如何将这些代数知识应用到RSA密码体制中. 一.公钥密码学简介 在经典密码学的研究模型中,我们根据已选择的秘钥K得到一条加密规则$e_{k}$和一条解 ...

  7. enctype="multipart/form-data"的form传参

    1.jsp <li class="btns"><input id="btnImport" class="btn btn-primar ...

  8. 【流畅的python】16 - 协程

    yield 产生是产生值给调用方 让步是暂停生成器,同时让步也可以作为流程控制手段 yield item 上面这行代码会产出一个值.提供给next(...)的调用方.此外还会做出让步,暂停执行生成器, ...

  9. Java超简明入门学习笔记(零)

    Java编程思想第4版学习笔记(零) 前言          这个笔记本主要记录了我在学习Java编程思想(第4版,中文版)的过程中遇到的重难点及其分析.主要参考了C++11版本的C++语言,对比了它 ...

  10. ElasticSearch入门之花落红尘(三)

    上篇文章散仙介绍了ElasticSearch的入门安装和使用,那么本篇我们来看下,如何使用java api来和ElasticSearch进行交互,简单点说,就是实现一个增删改查,来找找入门的感觉. 在 ...