开发者

Tokenizing String C++ compiler/logic error

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


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());
// ...


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


Change

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

to

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜