Apr 4, 2012 at 9:42pm UTC
I'm attempting to push_back several two-dimensional string vectors. However, I receive several error messages when I do. Here is an example of trying to push_back a string vector:
1 2 3
vector<vector<string> > myVector;
myVector.push_back(vector<string>());
myVector[0].push_back(1);
produces
error: invalid conversion from 'int' to 'const char*' [-fpermissive]
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/basic_string.tcc:214:5: error: initializing argument 1 of 'std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _All
oc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]' [-fpermissive]
I'm also pushing back several int vectors and a bool vector (all 2D) but it works fine with those. What's the problem with 2D string vectors?
Thanks!
P.S. My debugger seems to think the problem is on the last line of code.
Last edited on Apr 4, 2012 at 9:46pm UTC
Apr 4, 2012 at 9:44pm UTC
You're pushing an int, while the container is for a string. Just push it like that:
myVector[0].push_back("1" );
and it should work.
Last edited on Apr 4, 2012 at 9:45pm UTC
Apr 4, 2012 at 9:46pm UTC
1 is not a string so you can't store it in the string-vector.
Apr 4, 2012 at 10:01pm UTC
I thought the value in the () of push_back was the number of spaces you push it back. It's the value you add to the end??
Apr 4, 2012 at 10:04pm UTC
Yes, it is the actual value that gets copied to the end.
Apr 4, 2012 at 10:13pm UTC
Thanks, ha ha. I've never used vectors before but I'm glad I'm finally taking the time to learn how.