当我用字符串代替char时,代码可以完美地工作.为什么如此
The code works perfectly when I substitute char with string.why so?
下面的代码可以很好地处理字符串,但是对于字符,它会产生分段错误。
#include<iostream>
using namespace std;
class salary
{
public:
int empno;
float inctax;
float netsal;
int gross;
short int age;
char name[50];
salary(){
empno=0;
gross=0;
age=0;
strcpy(name,' ');
}
salary(int empn,int gros,short int ag,char nam[]){
empno=empn;
gross=gros;
age=ag;
strcpy(name,nam);
}
void calc(){
inctax=0.0;
if(gross>1000000)
inctax=0.3*gross;
else if(gross>=500000 && gross<=1000000)
inctax=0.2*gross;
else if(gross>=250000 && gross<500000)
inctax=0.1*gross;
else
inctax=0.0;
netsal=gross-inctax;
cout<<"inctax"<<inctax;
cout<<"net sal"<<netsal;
}
};
int main(){
salary *r=new salary();
salary *r1=new salary(112,500000,21,"Arnab");
r1->calc();
return 0;
}
我同意vu1p3n0x的评论,问题出在默认构造函数中。
strcpy(name, ' '); // This is wrong!
strcpy()接受两个字符数组作为参数,但在代码中,字符本身作为第二个参数传递。strcpy()的语法是:
char * strcpy ( char * destination, const char * source );
为了使用char数组创建空字符串,您可能应该使用
strcpy(name, " ");
/*or*/
name[0] = ' ';
更多关于复制string, Reference和tutorialpoint
您的主要问题是编译器没有告诉您代码中明显的错误。如果您已经正确地告诉它报告错误,那么您可能需要一个更好的编译器。下面是我编译时得到的输出:
g++ -std=c++14 -fPIC -g -Wall -Wextra -Wwrite-strings -Wno-parentheses 38113648.cpp -o 38113648
38113648.cpp: In constructor ‘salary::salary()’:
38113648.cpp:18:25: error: ‘strcpy’ was not declared in this scope
strcpy(name,' ');
^
38113648.cpp: In constructor ‘salary::salary(int, int, short int, char*)’:
38113648.cpp:25:24: error: ‘strcpy’ was not declared in this scope
strcpy(name,nam);
^
38113648.cpp: In function ‘int main()’:
38113648.cpp:46:48: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
salary *r1=new salary(112,500000,21,"Arnab");
^
38113648.cpp:45:13: warning: unused variable ‘r’ [-Wunused-variable]
salary *r=new salary();
^
添加
#include <cstring>
并将构造函数改为取char const[]
,得到
g++ -std=c++14 -fPIC -g -Wall -Wextra -Wwrite-strings -Wno-parentheses 38113648.cpp -o 38113648
38113648.cpp: In constructor ‘salary::salary()’:
38113648.cpp:19:25: warning: null argument where non-null required (argument 2) [-Wnonnull]
strcpy(name,' ');
^
显然你的意思是 " "
。更好的是,提供一个初始化式:
salary()
: empno{0},
gross{0},
age{0},
name{0}
{
}
salary(int empno, int gross, short int age, char const name[])
: empno{empno},
gross{gross},
age{age}
{
strcpy(this->name, name);
}
(我还给了形式参数更有意义的名字,因为这通常构成构造函数的文档)。
添加-Weffc++
也可能是值得的——在这种情况下,它警告你不要在构造函数中初始化inctax
或netsal
。如果您乐于使用部分初始化的对象,那么显然需要使用Valgrind来检查这些值在使用之前是否确实设置了,因为它不能静态地确定。
相关文章:
- QSqlquery prepare()和bindvalue()不工作
- 试试完美的正方形,你能给点小费吗
- 导入库可以跨dll版本工作吗
- 以螺旋方式打印矩阵的程序.(工作不好)
- 对象指针在c++中是如何工作的
- 为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作
- VSOMEIP-2个设备之间的通信(TCP/UDP)不工作
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- C++为线程工作动态地分割例程
- 为什么我的 std::ref 无法按预期工作?
- 布尔比较运算符是如何在C++中工作的
- SampleConsensusPrerejective(ext.RANSAC)是如何真正工作的
- 不确定要在我的main中放入什么才能使我的代码正常工作
- 为什么std::condition_variable notify_all的工作速度比notify_one快(对于随机请
- <<操作员在下面的行中工作
- 求出有多少个数字是完美平方,而sqrt()是L,R范围内的素数
- 有人能解释一下为什么下界是这样工作的吗C++的
- 为什么 std::function 不能与完美转发一起工作?
- C++ 昨天分段错误测试的代码工作完美,现在它出错了
- 当我用字符串代替char时,代码可以完美地工作.为什么如此