Solved

Question re: vectors

Posted on 2004-04-26
4
344 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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
is twain_32.dll cmpatible with windows 10 ? 10 238
I could not build boost code, 10 107
Recommendation vb6 to vb.net or others 14 209
IntelliJ and Eclipse Neon 2 installations 4 40
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

726 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