Solved

Question re: vectors

Posted on 2004-04-26
4
341 Views
Last Modified: 2013-12-14
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
Comment
Question by:jewee
  • 3
4 Comments
 
LVL 12

Expert Comment

by:stefan73
ID: 10916447
Hi jewee,
> fileTimeout = atoi(tokenList[2].c_str());

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


Cheers,
Stefan
0
 
LVL 12

Accepted Solution

by:
stefan73 earned 500 total points
ID: 10916489
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
 

Author Comment

by:jewee
ID: 10916601
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
 
LVL 12

Expert Comment

by:stefan73
ID: 10916650
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Reference external folders in Visual Studio 4 84
C++ Language error 28 245
Error creating a new C++ project in ,net 20 41
Gaming Software 1 19
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question