将char *无效转换为int

时间:2014-02-01 22:05:15

标签: c++

我被困在这一部分,我需要有关修复以下代码中的编译错误的帮助。我是一个使用g ++编译器。

错误消息如下:“从'char char *'到[-fpermissive]的无效转换”

bool isRegistered(const Prefix* pre,int area,const char* publisher, int * pos)
{
int n,i=0,num,flag=0,low,high,pub;
n=*(pos);
while(pre->area[n]==area)
{
num=publisher[i]-48;

switch(pre->pubLen[n])
{
case 1:

num=publisher[i]-48;
  

在每种情况下,问题都在下面。

low=((*pre).pubLow[n])-48;
high=(pre->pubHigh[n])-48; 
if((num>low)&&(num<high))
{
pub=num;
}
break;

case 2:
num=(publisher[i]-48)*10+(publisher[i+1]-48);
low=(pre->pubLow[n])-48;
high=(pre->pubHigh[n])-48;
if((num>low)&&(num<high))
{
pub=num;
}
break;
  

前缀的定义

struct Prefix {
    int  no;             // number of entries
    int  area[MAX];      // area elements
    char pubLow[MAX][8]; // low end of publisher range
    char pubHigh[MAX][8]; // high end of publisher range
    int  pubLen[MAX];    // no of chars in publisher string
};
  

案例3等等......

1 个答案:

答案 0 :(得分:0)

给出pubLow

的定义

另外

**low=((*pre).pubLow[n])-48;
high=(pre->pubLow[n])-48; **

low是一个整数,在低位之后和**之后删除48; 这样就变成了

low=((*pre).pubLow[n])-48;
high=(pre->pubLow[n])-48;

这是您的代码

 bool isRegistered(const Prefix* pre,int area, char* publisher, int * pos)
{
int n,i=0,num,flag=0,low,high,pub;
n=*(pos);
while(pre->area[n]==area)
{
num=publisher[i]-48;

switch(pre->pubLen[n])
{
case 1:

num=publisher[i]-48;
    low=atoi(((*pre).pubLow[n])-48);
high=atoi((pre->pubHigh[n])-48); 
if((num>low)&&(num<high))
{
pub=num;
}
break;

case 2:
num=(((publisher[i]-48)*10)+(publisher[i+1]-48));
low=atoi((pre->pubLow[n])-48);
high=atoi((pre->pubHigh[n])-48);
if((num>low)&&(num<high))
{
pub=num;
}
break;