c++:warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
来源:互联网 发布:mysql sql语句 编辑:程序博客网 时间:2024/06/14 13:30
首先,它任然是有效的C++代码,应为你写的char* 是具有c风格的字符串,所以g++不识别
可以选择在编译的时候加上:g++ -Wno-write-strings text.cpp //忽略警告。。。
其实这样是并不是很安全
上代码
1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 using namespace std; 5 6 class Student{ 7 private: 8 int score; 9 char *name;//可变的变量 10 public: 11 Student(const char *name,int score); 12 Student(Student& stu); 13 ~Student(); 14 void show(); 15 }; 16 17 Student::Student(const char *name1,int score1)//这里改了const 18 { 19 cout<<"constructing ..."<<endl; 20 21 name = new char[strlen(name1)+1]; 22 if(name != 0) 23 { 24 strcpy(name,name1); 25 score = score1; 26 } 27 } 28 29 Student::~Student() 30 { 31 cout<<"Destructing..."<<endl; 32 name[0] = '\0'; 33 delete name; 34 } 35 36 Student::Student(Student& stu) 37 { 38 cout<<"copy constructing ..."<<endl; 39 name = new char[strlen(stu.name)+1]; 40 if(name != 0) 41 { 42 strcpy(name,stu.name); 43 44 score=stu.score; 45 } 46 } 47 48 void Student::show() 49 { 50 cout<<name<<endl; 51 cout<<score<<endl; 52 } 53 54 int main() 55 { 56 Student stu1("huhao",101);//在这里传入的是const的变量 57 /* 58 Student stu2=stu1; 59 stu1.show(); 60 stu2.show();*/ 61 }
0 0
- warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
- warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
- warning:deprecated conversion from string constant to 'char *'[-Wwrite-strings]
- warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
- [C/C++]warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]|
- c++:warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
- warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-st
- 解决C++中[Warning] deprecated conversion from string constant to 'char*' [-Wwrite-strings]
- warning: deprecated conversion from string constant to ‘char的解决方法
- warning: deprecated conversion from string constant to ‘char*’
- warning: deprecated conversion from string constant to "char *"
- warning:deprecated conversion from string constant to 'char *'
- warning: deprecated conversion from string constant to 'char*'
- warning:deprecated conversion from string constant to 'char *'解决方案
- warning: deprecated conversion from string constant to ‘char*’
- warning: deprecated conversion from string constant to "char *"
- warning: deprecated conversion from string constant to "char *"
- warning: deprecated conversion from string constant to "char *"
- [CodeForces 567D] One-Dimensional Battle Ships (区间隔断计数)
- 原型图登录优化
- 欢迎来到我的博客
- 序列化和反序列化
- Ubuntu12.04中weka的使用
- c++:warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
- C++语法细节-静态成员
- Eclipse下导入PagerSlidingTabStrip的解决方案
- Spring总结2—IOC
- 第八周上机实践项目:矩阵元素变换
- android自定义View之仿通讯录侧边栏滑动,实现A-Z字母检索
- Adblock的简介、安装和使用
- A1330. 世博会(刘洪轩) 主席树
- 在 Qt 中搭建 qwt (超详版)