无法从“const char *”转换为“char

Love_TuRong 2009-04-07 02:06:06
soap_QName2s(struct soap *soap, const char *s)
{ struct Namespace *p;
char *t;
int n;
if (!s || *s != '"')
{
#ifndef WITH_LEAN
if (s && (soap->mode & SOAP_XML_CANONICAL))
{ t = strchr(s, ':');//这里提示错误
if (t)
soap_utilize_ns(soap, s, t - s);
}
#endif
return s;
}

……
这段代码有问题吗?
...全文
1847 14 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
Love_TuRong 2009-04-07
  • 打赏
  • 举报
回复
12楼 写错了 应该是这个样子的:

struct Namespace *p;
char *t;
int n;
if (!s || *s != '"')
{
#ifndef WITH_LEAN
if (s && (soap->mode & SOAP_XML_CANONICAL))
{ t = strchr(s, ':');
if (t)
soap_utilize_ns(soap, s, t - s);
}
#endif
return s;
}

谢谢大家
beyond071 2009-04-07
  • 打赏
  • 举报
回复
如果是这样,那只有强制类型转换了

#ifndef WITH_LEAN
if (s && (soap->mode & SOAP_XML_CANONICAL))
{t = (char*)strchr(s, ':'); //这里强制转换成char*
if (t)
soap_utilize_ns(soap, s, t - s);
}
#endif
Love_TuRong 2009-04-07
  • 打赏
  • 举报
回复
谢谢各位的指导 这是gsoap里面的函数
请看:
soap_QName2s(struct soap *soap, const char *s)
{ struct Namespace *p;
char* t;
int n;
if (!s || *s != '"')
{
#ifndef WITH_LEAN
if (s && (soap->mode & SOAP_XML_CANONICAL))
{const char *t = strchr(s, ':');
if (t)
soap_utilize_ns(soap, s, t - s);
}
#endif
return s;
}
s++;
if ((p = soap->local_namespaces))
{ for (; p->id; p++)
{ if (p->ns)
if (!soap_tag_cmp(s, p->ns))
break;
if (p->in)
if (!soap_tag_cmp(s, p->in))
break;
}
if (p && p->id)
{ s = strchr(s, '"');
if (s)
{ t = (char*)soap_malloc(soap, strlen(p->id) + strlen(s));
strcpy(t, p->id);
strcat(t, s + 1);
return t;
}
}
}
t = (char*)strchr(s, '"');
if (t)
n = t - s;
else
n = 0;
t = soap_strdup(soap, s);
t[n] = '\0';
sprintf(soap->tmpbuf, "xmlns:_%d", soap->idnum++);
soap_set_attr(soap, soap->tmpbuf, t);
s = strchr(s, '"');
if (s)
{ t = (char*)soap_malloc(soap, strlen(soap->tmpbuf) + strlen(s) - 6);
strcpy(t, soap->tmpbuf + 6);
strcat(t, s + 1);
}
return t;
}
beyond071 2009-04-07
  • 打赏
  • 举报
回复
C++中,strchr有两个重载:char* strchr(char*, int); const char* strchr(const char*, int);由于楼主传入的s是const char*,因此使用的是const版本的重载,所以t必须也定义成const char*。
bfhtian 2009-04-07
  • 打赏
  • 举报
回复
需要你自己从const char *转换为char*,编译器确实不支持这样的默认转换
Love_TuRong 2009-04-07
  • 打赏
  • 举报
回复
vs2005
beyond071 2009-04-07
  • 打赏
  • 举报
回复

int ch = ':';
const char* t = strchr( s, ch ); //使用const的版本
Love_TuRong 2009-04-07
  • 打赏
  • 举报
回复
vs2005
beyond071 2009-04-07
  • 打赏
  • 举报
回复
请问是使用的编译平台是?
Love_TuRong 2009-04-07
  • 打赏
  • 举报
回复
问题是我用的时候提示错误
beyond071 2009-04-07
  • 打赏
  • 举报
回复
const char *s 是函数soap_QName2s的const类型参数
extern char *strchr(char *s,char c);
strchr用的是char*类型。
Love_TuRong 2009-04-07
  • 打赏
  • 举报
回复
唉 这是gsoap里面的
不知道为什么我用的时候老是在这里提示错误
ryfdizuo 2009-04-07
  • 打赏
  • 举报
回复
sorry 是const char* s
晕死、
ryfdizuo 2009-04-07
  • 打赏
  • 举报
回复
函数参数是const char* t
t在函数里面不可以修改,

65,187

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧