标记字符串 C++编译器/逻辑错误
在下面的第一行代码中我得到了这个 错误:无法将参数 '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 弦和 & 演奏。 C++ 字符串,没有太多区分。
strtok也是String.h的静态方法吗?如果我想传递另一个字符串来标记怎么办?
谢谢,马凯尔·贝尔
At the following first line of code I get this
error: cannot convert ‘std::string’ to ‘char*’ for argument ‘1’ to ‘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];
}
}
Full code:
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);
}};
Am I passing the wrong type I string to strtok. I know I am playing with C-string and & C++ string without distinguishing them much.
Also is strtok a static method of String.h? What If I want to pass it another string to tokenize?
Thanks, Macaire Bell
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
strtok
需要可变的 C 字符串,而不是std::string
。要从std::string
获取 C 字符串,请使用c_str()
方法。但是,您不应该不将其传递给strtok
,因为它不应该被更改。您需要复制该字符串。为了创建副本,可能的方法是:
或使用 std::vector:
strtok
needs mutable C string, notstd::string
. To get a C string fromstd::string
usec_str()
method. However you should not pass it tostrtok
because it should not be changed. You'll need to make a copy of the string.In order to create a copy possible way would be:
or with std::vector:
strtok()
是来自 C 标准库的野兽,C++ 通过继承获得了它。从 C++ POV 来看,这是不安全的,应该避免。在 C++ 中标记以空格分隔的字符串的方法是使用字符串流。大致:strtok()
is a beast from the C Standard library, which C++ got by inheritance. From a C++ POV, it's unsafe and should be avoided. The way to tokenize a whitespace-separated string in C++ is to use string streams. Roughly:更改
为
Change
to