-1

Eclipse shows the following Error on the push_back line:

Invalid arguments ' Candidates are: void push_back(const std::basic_string<char,std::char_traits<char>,std::allocator<char>> &)

My Question is, how can i make sure that the vector contains strings and not basic_strings?

I have figured out in another piece of code where i wanted to access this vector that i have troubles compiling it, it seems to know later that it is a basic_string and not a "std::string", in the current context i know that i only will use char but i would like to keep the superclass in there so that in the future i don't have to adjust anything if i swap to w-char ect.

I have already tried casting it to a string but that seems not to work

 std::string test = (std::string)tokens[1];

The final goal would be to construct some object with that vector

if(tokens->size() == 3){
TestObject test = new TestObject(tokens[1],)tokens[2])
}

This is the Function i have found on Stackoverflow which was proposed by a stackoverflow user Function Origin

Thanks for helping.

void TestClass::splitString(const std::string &str, std::vector<std::string>* tokens){
    boost::char_separator<char> sep(",");

    std::string separator;
    boost::tokenizer< boost::char_separator<char> > tok(str,sep);
    for(boost::tokenizer< boost::char_separator<char> >::iterator it=tok.begin(); it != tok.end(); ++it)
    {    
      tokens->push_back(*it);
    }    
}

no matching function for call to ‘TestObject::defineX(std::basic_string*)’

accessing with defineX(vector[1].data());

no matching function for call to ‘TestObject::defineX(std::vector >&)’

accessing with defineX(vector[1]);

The definition of the function is declared as the following(maybe this is the issue?):

typedef std::string td_MAP_KEY;
td_MAP_VALUE * TestObject::defineX(td_MAP_KEY key);

I call the method like this, if your keen to compile this, it should work, unfortunately i' am not able to paste the complete code here but the code below should doit:

std::string x = "one,two,three,four"
std::vector<std::string> * splittedString = new std::vector<std::string>;
splitString(x,splittedString);
TestObject * object = new TestObject();
object->defineX(splittedString[1]);
Community
  • 1
  • 1
Oliver
  • 928
  • 9
  • 25
  • 2
    `std::string` is a typedef for `std::basic_string,std::allocator>` – Benjamin Lindley Jul 21 '12 at 15:19
  • 1
    Where is the function with error you are getting? Is it that splitString function? Because it works fine for me. – Benjamin Lindley Jul 21 '12 at 15:31
  • @BenjaminLindley i get the error when i try to access the elements in this vector directly because it will give me back a basic_string and the compiler will complain that it is not a std::string, i believe its not a syntax error but as defined in the array it should be std::string and not a basic_string, casting didn't help either, in addition iam not sure because we construct the tokenizer locally but pass the pointer address to the vector, after the function it will be destructed except the push_back will make a copy of it. – Oliver Jul 21 '12 at 17:01
  • Please give a complete example that demonstrates the problem, not a description in English. Like I said, `std::string` is a form of `std::basic_string`, so it doesn't make any sense to say *"it should be std::string not a basic_string"*. – Benjamin Lindley Jul 21 '12 at 17:08
  • @BenjaminLindley i have added Code you wanted, in case you compile it too, i have g++ 4.6.3 – Oliver Jul 21 '12 at 18:22
  • @BenjaminLindley i have tried with the vector->at(1), this seems to work i guess my mistake was that the vector was a pointer and that i didn't dereference it (*splittedString)[1] or splittedString->at(1) works. – Oliver Jul 21 '12 at 18:32

1 Answers1

0

The problem with this code:

std::vector<std::string> * splittedString = new std::vector<std::string>;
    :
object->defineX(splittedString[1]);

is that you define splittedString as a vector pointer, and point it at a single vector (rather than an array of vectors), and then try to access the second element of the (non-existent) array of vectors. You probably want

object->defineX((*splittedString)[1]);

which dereferences the vector pointer and then grabs the second element of the vector.

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226