在下面的第一行代码中,我得到这个 错误:无法将参数'1'的'std :: string'转换为'char *'到'char * strtok(char *为const char *)”Tokenizing String C++编译器/逻辑错误
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);
}};
我是传递了错误的类型我字符串函数strtok。我知道我在玩C字符串和C++字符串,但没有太多区别。
也是strtok一个String.h的静态方法吗?如果我想通过它另一个字符串来标记?
感谢,MACAIRE贝尔
“你需要复制一个字符串。” - >最好用'std :: vector'我想。 –
fredoverflow
2010-11-21 18:42:49
@Drakosha如果我理解正确: 如果我使用c_str()它会将我的字符串更改为一个cstring? 但是,因为我只是将它复制到一个矢量字符串作为令牌,我应该创建一个本地副本? – Malaken 2010-11-21 18:43:19
@Malaken:'std :: basic_string :: c_str()'返回一个指向字符串中保存的数据的**常量**副本的指针。对于大多数STL实现来说,这将与字符串内缓冲区中实际保存的数据相同,但这绝不是必需的。基本上,你不能使用'c_str'返回的指针以任何方式改变指针所指向的缓冲区的内容(否则你有未定义的行为) –
2010-11-21 18:54:25