Solved

Question re: vectors

Posted on 2004-04-26
4
335 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now