在这里,我试图将字符串 tocken 复制到 char 指针,如下所示:
#include <iostream>
#include <cstring>
#include <string>
#include <boost/tokenizer.hpp>
using namespace std;
using namespace boost;
int main(int, char**)
{
string text = "token test string";
char *word;
char_separator<char> sep(" ");
tokenizer<char_separator<char>> tokens(text, sep);
int i=0;
for (const auto& t : tokens) {
cout << t << "." << endl;
word[i] =(const char *)strdup(t); // Error
i++; } }
错误是:test.cpp:18:40: error: cannot convert ‘const std::basic_string<char>’ to ‘const char*’ for argument ‘1’ to ‘char* strdup(const char*)