20

I'm trying to convert a std::string stored in a std::vector to an integer and pass it to a function as a parameter.

This is a simplified version of my code:

vector <string> record;
functiontest(atoi(record[i].c_str));

My error is as follows:

error: argument of type ‘const char* (std::basic_string<char, std::char_traits<char>, std::allocator<char> >::)()const’ does not match ‘const char*’

How can I do this?

CodeMouse92
  • 6,840
  • 14
  • 73
  • 130
Daniel Del Core
  • 3,071
  • 13
  • 38
  • 52

4 Answers4

44

With C++11:

int value = std::stoi(record[i]);
Pete Becker
  • 74,985
  • 8
  • 76
  • 165
11
record[i].c_str

is not the same as

record[i].c_str()

You can actually get this from the error message: the function expects a const char*, but you're providing an argument of type const char* (std::basic_string<char, std::char_traits<char>, std::allocator<char> >::)()const which is a pointer to a member function of the class std::basic_string<char, std::char_traits<char>, std::allocator<char> > that returns a const char* and takes no arguments.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
11

Use stringstream from standard library. It's cleaner and it's rather C++ than C.

int i3;
std::stringstream(record[i]) >> i3; 
Indy9000
  • 8,651
  • 2
  • 32
  • 37
0
#include <boost/lexical_cast.hpp>

functiontest(boost::lexical_cast<int>(record[i]));
Darko Veberic
  • 451
  • 1
  • 6
  • 12