[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 353
  • Last Modified:

Question re: vectors

I am trying to set a value from a vector, convert it using atoi then saving it to an int:

 std::vector<string> tokenList(3);

fileTimeout = atoi(tokenList[2].c_str());

I get an error with and without the c_str:

testThis.cpp:29: no matching function for call to `atoi(std::basic_string<char,
   std::char_traits<char>, std::allocator<char> >&)'
0
jewee
Asked:
jewee
  • 3
1 Solution
 
stefan73Commented:
Hi jewee,
> fileTimeout = atoi(tokenList[2].c_str());

Try
fileTimeout = atoi((tokenList[2]).c_str());


Cheers,
Stefan
0
 
stefan73Commented:
jewee,

Hmm, this compiles fine:

#include <string>
#include <vector>
#include <stdlib.h>

using namespace std;

int main(){
       std::vector<string> tokenList(3);

      int fileTimeout = atoi(tokenList[2].c_str());
      return 0;
}      

did you include stdlib?

Stefan
0
 
jeweeAuthor Commented:
Uggh.  Unbelievable.  Thanks!  One more quick question.  I am trying to set another token:

char fileDir[256];

fileDir = tokenList[2];

incompatible types in assignment
0
 
stefan73Commented:
jewee,
> fileDir = tokenList[2];

Try if you can use a string instead of your char array.

If not, you need strcpy() for the c_str():

#include <strings.h>
strcpy(fileDir,tokenList[2].c_str());

...but that's C-style code. There is no check for buffer overflows, for example.

Stefan
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now