Possible Duplicate:
C++ convert char to const char*为什么错误“从'char'无效转换为'const char *'?
我有一个代码段:
string opposite(string c)
{
if(c == (string) "\""){return "\"";}
if(c == (string) "<"){return ">";}
throw;
}
int load_end(int start, string code)
{
//start is the begining of "header.h" or <header> in #load "header.h" or #load <header>
//code is self explanitory
//This function returns the end of "header.h" or <header> in #load "header.h" or #load
string chr = " ";
int e;
string asdf = opposite(code[start]);
for(int i = start; chr == asdf; i++)
{
e = i;
chr = code[i];
}
return e;
}
在其中,其中,其中“ASDF”被定义错误的字出现线; “无效的从'char'转换为'const char *'[-fpermissive]”另外还有一个错误:“c:\ mingw \ bin .. \ lib \ gcc \ mingw32 \ 4.6.2 \ include \ C++ \ bits \ basic_string.tcc | 214 |错误:初始化参数1'std :: basic_string < _CharT,_Traits,_Alloc> :: basic_string(const _CharT *,const _Alloc &)[with _CharT = char,_Traits = std :: char_traits,_Alloc =标准::分配器] - [-fpermissive] |。”这些错误没有什么意义我他们是怎么引起的
你使用'std :: string',还是你自己的?如果它是'std :: string',则不需要在'opposite'中投入,因为'c ==“\”“'和'c ==”<“'会隐式地将后面的参数转换为一个字符串比较 – chris
'&code [开始]'将返回一个字符,并将被存储在'字符串C' ....更改'字符串C'到'字符' –
我所有[GCC 4.7.2] (http://liveworkspace.org/code/2B8IRC$0)是'warning:控制达到非void函数的结束[-Wreturn-type]'和'warning:'e'可能在此函数中未初始化使用[-Wmaybe第一个适用于'opposite',没有'return'语句,第二个适用于for循环可能不执行的事实,使'e'未初始化。 – chris