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. linux 解压 WinRAR 压缩文件

    1.Download rar for linux wget http://www.rarlab.com/rar/rarlinux-x64-5.5.b1.tar.gz 2.Configure rar t ...

  2. CF961F k-substring

    题意:给你一个字符串(sl<=1e6),问每一个起点到1和终点到sl距离相等的子串的最长不等于串长的border. 标程: #include<cstdio> #include< ...

  3. data方法也是模型类的连贯操作方法之一,

    data方法也是模型类的连贯操作方法之一,用于设置当前要操作的数据对象的值. 写操作 通常情况下我们都是通过create方法或者赋值的方式生成数据对象,然后写入数据库,例如: $Model = D(' ...

  4. 一台电脑同时添加git和bitbucket两个网站的ssh key

    添加第一个ssh key 就不多说了,不懂的可以自己查资料 ssh-keygen -t rsa -C 'email_1@email.com' 然后一路enter就好了 假设已经添加好了git的ssh ...

  5. linux mysql备份shell

    #!/bin/bash # Shell script to backup MySql database # Author: Henry he # Last updated: -- # crontab ...

  6. 解决git每次输入密码,设置gitlab、github默认push的用户名和密码

    git ssh key配置&解决git每次输入密码   欢迎加入qq群(IT-程序猿-技术交流群):757345416 在使用git时,每次pull/push都需要输入密码,有时大大降低了我们 ...

  7. IoC深入理解

    1. IoC理论的背景 我们都知道,在采用面向对象方法设计的软件系统中,它的底层实现都是由N个对象组成的,所有的对象通过彼此的合作,最终实现系统的业务逻辑. 图1:软件系统中耦合的对象 如果我们打开机 ...

  8. 数论专场 Day9 部分题解

    // 2019年西电暑期集训 // [7月9日]基础数论:https://cn.vjudge.net/contest/309097 A - Visible Lattice Points 题目大意: 平 ...

  9. Ubuntu安装Maven(转)

    原文地址:http://my.oschina.net/hongdengyan/blog/150472 一.环境说明: 操作系统:Ubuntu 14.10(64位) maven:apache-maven ...

  10. elasticsearch 中文API(二)

    客户端 有多个地方需要使用Java client: 在存在的集群中执行标准的index, get, delete和search 在集群中执行管理任务 当你要运行嵌套在你的应用程序中的Elasticse ...