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. 迭代器/生成器/装饰器 /Json & pickle 数据序列化

    本节内容 迭代器&生成器 装饰器 Json & pickle 数据序列化 软件目录结构规范 作业:ATM项目开发 1.列表生成式,迭代器&生成器 列表生成式 孩子,我现在有个需 ...

  2. postgresql数据库安装后的pgadmin4切换中文

    如图所示

  3. Python3基础笔记_字符串类型

    # 1.Python转义字符 a = "sqwerdf" # 2.Python字符串运算符 ''' + 字符串连接 a + b 输出结果: HelloPython * 重复输出字符 ...

  4. thinkphp 数据写入

    直线电机优势 ThinkPHP的数据写入操作使用add方法,使用示例如下: $User = M("User"); // 实例化User对象 $data['name'] = 'Thi ...

  5. python Xls文档读写

    1.模块安装 2.python 代码 import xlrd import xlwt import datetime def set_style(name,height,format,bold=Fal ...

  6. centOS7.2防火墙常用配置(转)

    centOS7.2防火墙常用配置   firewall-cmd --state #查看默认防火墙状态(关闭后显示not running,开启后显示running) systemctl stop fir ...

  7. Centos--swoole平滑重启服务

    平滑重启: 已经打开的服务: 首先在server服务中为进程添加名字: /** * @param $server */ public function onStart($server) { swool ...

  8. Windows进程创建的流程分析

    .   创建进程的大体流程:   创建进程的过程就是构建一个环境,这个环境包含了很多的机制 (比如自我保护, 与外界通信等等). 构建这个环境需要两种"人"来协调完成(用户态和内核 ...

  9. 2019暑训第一场训练赛 |(2016-icpc区域赛)部分题解

    // 今天下午比赛自闭了,晚上补了题,把AC的部分水题整理一下,记录坑点并吸取教训. // CF补题链接:http://codeforces.com/gym/101291 A - Alphabet 题 ...

  10. 分布式配置中心(Spring Cloud Config)

    真有意思的一个问题,我先把我遇到的写一次 ,今天学习Spring Cloud Config  新建了三个module ,eureka-server,config-server,config-clien ...