标记字符串 C++编译器/逻辑错误

发布于 2024-10-03 06:37:36 字数 2934 浏览 0 评论 0原文

在下面的第一行代码中我得到了这个 错误:无法将参数 '1' 的 'std::string' 转换为 'char*' 到 'char* strtok(c​​har*, 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

帝王念 2024-10-10 06:37:36

strtok 需要可变的 C 字符串,而不是 std::string。要从 std::string 获取 C 字符串,请使用 c_str() 方法。但是,您不应该将其传递给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());
// ...

strtok needs mutable C string, not std::string. To get a C string from std::string use c_str() method. However you should not pass it to strtok 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:

std::string strToTokenize;
char * tmpStr = new char[strToTokenize.size() + 1];
if (NULL == tmpStr) {...}
strcpy(tmpStr, strToTokenize.c_str());
// tokenize tmpStr
// ...
delete [] tmpStr;

or with 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());
// ...
南笙 2024-10-10 06:37:36

strtok也是String.h的静态方法吗?如果我想传递另一个字符串来标记怎么办?

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!";

Also is strtok a static method of String.h? What If I want to pass it another string to tokenize?

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:

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!";
兔姬 2024-10-10 06:37:36

更改

TokenLine[0]= strtok (Line," ");

TokenLine[0]= strtok (Line.c_str()," ");

Change

TokenLine[0]= strtok (Line," ");

to

TokenLine[0]= strtok (Line.c_str()," ");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文