在下面的第一行代码中我得到了这个 错误:无法将参数'1'的'std :: string'转换为'char *'为'char * strtok(char *,const char *)'
void ToToken( ) {
TokenLine.reserve(30);
char * tmp;
TokenLine[0]= strtok (Line," ");
while(tmp!=NULL)
for(int i=0;i<TokenLine.size();i++){
TokenLine[i]= strtok(NULL," ");
if(TokenLine[i]==NULL||TokenLine[i]==" ")
TokenLine.erase(i);
cout<<Token[i];
}
}
完整代码:
class CombatLine{
string Line;
bool combat;
char LT[4];
time_t rawtime;
vector<string> TokenLine;
CombatLine(){
combat = false;
}
void SetLine(string S){
Line="[Combat] 03:33:05 -Anthrax- Roshi heals -Anthrax- Roshi for 2630 points of damage.";
}
bool isLineCombat(){
if(Line.substr(0,8)=="[Combat]")
return true;
else
return false;
}
bool StrFound(string SubString){
size_t tmp;
tmp = Line.find(SubString);
if(tmp!=string::npos)
return true;
else
return false;
}
void SetLT(){
LT[0]=Line.at(13);
LT[1]=Line.at(14);
LT[2]=Line.at(16);
LT[3]=Line.at(17);
}
char ReturnLT(int Index){
return LT[Index];
}
void SetType(){
if (this->StrFound("(dodge)"))
Event.SetType("dodge");
if (this->StrFound(" (parry) "))
Event.SetType("parry");
if(this->StrFound("misses"))//because we know its not a parry or dodge if we made it this far
Event.SetType("miss");
if(this->StrFound(" strikes through "))
Event.SetType("st");
if(this->StrFound("(evaded)"))
Event.SetType("evade");
if(this->StrFound("crits"))
Event.SetType("crit");
if(this->StrFound("hits"))
Event.SetType("hit");
if(this->StrFound("glances"))
Event.SetType("glance");
else
Event.SetType("not found");
}
void ToToken(){
TokenLine.reserve(30);
char * tmp;
TokenLine[0]= strtok (Line," ");
while(tmp!=NULL)
for(int i=0;i<TokenLine.size();i++){
TokenLine[i]= strtok(NULL," ");
if(TokenLine[i]==NULL||TokenLine[i]==" ")
TokenLine.erase(i);
cout<<Token[i];
}
}
string ReturnType(){
this->SetType();
return Event.ReturnType();
}
void SetMinMax(){
if(Event.ReturnType()=="miss"||Event.ReturnType()=="dodge"||Event.ReturnType()=="parry")
Event.SetMinMax(0,0);
}};
我将错误的类型I字符串传递给strtok。我知道我正在玩C弦和&amp; C ++字符串没有区别很多。
另外strtok是String.h的静态方法吗?如果我想将另一个字符串传递给tokenize?
谢谢,Macaire Bell
答案 0 :(得分:3)
strtok
需要可变的C字符串,而不是std::string
。从std::string
使用c_str()
方法获取C字符串。但是,您应不将其传递给strtok
,因为它不应更改。你需要复制一下这个字符串。
为了创建一个可能的副本方式:
std::string strToTokenize;
char * tmpStr = new char[strToTokenize.size() + 1];
if (NULL == tmpStr) {...}
strcpy(tmpStr, strToTokenize.c_str());
// tokenize tmpStr
// ...
delete [] tmpStr;
或使用std :: vector:
std::string strToTokenize;
std::vector<char> tmpStr = strToTokenize; // don't have a compiler now, probably won't work
// tokenize char * cStrToTokenize = &(*strToTokenize.begin());
// ...
答案 1 :(得分:0)
另外strtok是String.h的静态方法吗?如果我想将另一个字符串传递给tokenize?
strtok()
是来自 C 标准库的野兽,C ++通过继承得到它。从C ++ POV来看,它是不安全的,应该避免。在C ++中标记空格分隔字符串的方法是使用字符串流。粗略地说:
std::istringstream iss(Line);
std::string token;
while(iss >> token) // fails on error and EOF
process(token);
if(!iss.eof()) // failed on error
throw "Dude, you need error management!";
答案 2 :(得分:-3)
更改
TokenLine[0]= strtok (Line," ");
到
TokenLine[0]= strtok (Line.c_str()," ");