带有指针的字符传递函数
char passing functions with pointers
当我用 Turbo C++ 编写这个程序时,它工作正常。但是,当我在 CodeBlocks、Xcode 中编写时,我收到一个关于 char
的错误,我不知道为什么。我想我可以声明类似 char* name
的东西用作字符串。
该程序是关于多级继承的。将利率和期限传递给函数,它将根据帐户类型显示输出。
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
class banktype1{
public:
char *accountType;
float interestAmt,depositAmt,period,totalAmt;
public:
void interestCal(char *x,int y,int z){
accountType=&x;
depositAmt=y;
period=z;
if(accountType=="A")
interestAmt=depositAmt*0.5*period;
else if(accountType=="B")
interestAmt=depositAmt*0.15*period;
else if(accountType=="C")
interestAmt=depositAmt*0.25*period;
}
};
class banktype2:public banktype1{
public:
void displayData(){
cout<<interestAmt<<"n"<<depositAmt<<endl;
cout<<"Total"<<interestAmt+depositAmt;
}
};
class banktype3:public banktype2{
};
int main(){
banktype3 b1;
b1.interestCal("A",1000,12);
b1.interestCal("B",1000,12);
b1.interestCal("C",1000,12);
b1.displayData();
return 0;
}
在调用函数的地方,我收到以下通知:
不推荐从字符串文本转换为字符 *。
以及在我得到的 if 条件的地方:
未指定与字符串文本的比较结果(请改用 strncmp)
使用 std::string
而不是旧的 C 字符串,后者只是字符数组。
请注意,相等运算符只是在 C 字符串的情况下比较数组的地址,但在 std::string
的情况下,它重载以做字符串比较。(所以你的 elseif 代码应该使用 std::string
)。
另请注意,使用 C 字符串时,应使用 const char*
,切勿char*
。
解决方案很简单(请记住,这是一个很好的经验法则):除非在极少数情况下,它是必不可少的,否则始终使用 C++ 功能而不是其 C 等效项,在这种情况下,只需使用 std::string
.它旨在让您的生活更轻松。
C++中的字符串文字具有常量字符 arrray 的类型。
但无论如何函数定义
void interestCal(char *x,int y,int z){
accountType=&x;
depositAmt=y;
period=z;
if(accountType=="A"){
interestAmt=depositAmt*0.5*period;
}else if(accountType=="B"){
interestAmt=depositAmt*0.05*period;
}else if(accountType=="C"){
interestAmt=depositAmt*0.05*period;
}
}
是错误的。
正如编译器警告的那样,如果将字符串文本作为参数传递给函数,则第一个参数应具有类型 const char *
。
此声明
accountType=&x;
无效。右操作数&x
具有类型 char **
,而左操作数具有类型 char *
因为帐户类型声明为
char *accountType;
这
if(accountType=="A"){
和其他 if else 语句也无效。在这里,您正在尝试比较指针。
如果您将帐户类型定义为
char accountType;
并且该函数看起来像
void interestCal( char x, int y, int z )
{
accountType = x;
depositAmt = y;
period = z;
if ( accountType == 'A' )
{
interestAmt = depositAmt * 0.5 * period;
}
else if ( accountType == 'B' )
{
interestAmt = depositAmt * 0.05 * period;
}
else if( accountType == 'C' )
{
interestAmt = depositAmt * 0.05 * period;
}
}
它将从主调用为
b1.interestCal( 'A', 1000, 12 );
b1.interestCal( 'B', 1000, 12 );
b1.interestCal( 'C', 1000, 12 );
此外,如果三个 if-else 语句确实具有相同的复合语句,那么它们可以重写为一个 if 语句
if ( accountType == 'A' || accountType == 'B' || accountType == 'C')
{
interestAmt = depositAmt * 0.05 * period;
}
啊,它奏效了。 根据答案进行一些更改后。 删除const
. 并将 if(accountType=='A') 更改为 if(*accountType=='A')
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
class banktype1{
public:
char *accountType;
float interestAmt,depositAmt,period,totalAmt;
public:
void interestCal(char x,int y,int z){
accountType=&x;
depositAmt=y;
period=z;
if(*accountType=='A'){
interestAmt=depositAmt*0.5*period;
}else if(*accountType=='B'){
interestAmt=depositAmt*0.05*period;
}else if(*accountType=='C'){
interestAmt=depositAmt*0.05*period;
}
}
};
class banktype2:public banktype1{
public:
void displayData(){
cout<<interestAmt<<"n"<<depositAmt<<endl;
cout<<"Total"<<interestAmt+depositAmt;
}
};
class banktype3:public banktype2{
};
int main(){
banktype3 b1;
b1.interestCal('A',1000,12);
b1.interestCal('B',1000,12);
b1.interestCal('C',1000,12);
b1.displayData();
return 0;
}
- 将字符串数组传递给接受常量字符**的函数
- 通过键入的引用问题传递函数
- 如何将字符传递到find_child_by_attribute中以获取 pugixml
- 通过两个嵌套函数传递C++函数的名称
- 如何从模板类传递函数
- 通过像printf这样的可变参数函数传递一个带有常量字符*转换函数的类
- 从私有函数传递函数作为参数
- 为什么采用 int 的函数重载优先于采用无符号字符的函数重载?
- 如何通过模板传递函数?
- 我在使用字符的函数时遇到了一些问题
- 常量字符[] 默认函数参数
- 传递函数指针时未解析的外部符号
- 字符串文字可以传递给接受常量字符*的函数吗?
- 将字符串的字符传递到C 中的函数
- 为什么我在尝试将字符传递给我的函数以及如何修复它时会获得 SIGSEGV
- 将单个字符传递到C 中的函数
- 字符传递给其他函数最佳实践
- 如何将字符传递给期望字符串的函数
- 从字符串转换为字符* - 传递函数指针
- 带有指针的字符传递函数