À l'aide de push_back sur un vecteur de chaînes de caractères - C++

Je suis en train d'utiliser push_back sur un vecteur de chaînes de caractères en C++. Comment puis-je pousser un caractère unique sur le vecteur? Actuellement, j'ai essayé ce qui suit, toutes sans succès:

  1. Initialiser une chaîne de caractères (essayé de) avec le personnage.

Code

string str(main_string[0]);  
vector_string.push_back(str);
  1. Essayé d'appeler la fonction strcpy et donc de copier le contenu. Const-ness semble obtenir de la manière.

Code

string str;  
strcpy(main_string[0], str.c_str());  
vector_string.push_back(str);  

Plus des suggestions/idées sont la plupart de bienvenue.

Edit: Les journaux d'erreurs sont comme suit:

test_push.C: In function void test_push(std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >&, std::string)’:
test_push.C:50: error: no matching function for call to std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char&)’
/usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/bits/basic_string.tcc:220: note: candidates are: std::basic_string<_CharT, _Traits, _Alloc>::basic_string(typename _Alloc::rebind<_CharT>::other::size_type, _CharT, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
/usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/bits/basic_string.tcc:213: note:                 std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>] <near match>
/usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/bits/basic_string.tcc:206: note:                 std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, typename _Alloc::rebind<_CharT>::other::size_type, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
/usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/bits/basic_string.tcc:194: note:                 std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const std::basic_string<_CharT, _Traits, _Alloc>&, typename _Alloc::rebind<_CharT>::other::size_type, typename _Alloc::rebind<_CharT>::other::size_type, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
/usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/bits/basic_string.tcc:184: note:                 std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const std::basic_string<_CharT, _Traits, _Alloc>&, typename _Alloc::rebind<_CharT>::other::size_type, typename _Alloc::rebind<_CharT>::other::size_type) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
/usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/bits/basic_string.tcc:170: note:                 std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
/usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/bits/basic_string.tcc:178: note:                 std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
/usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/bits/basic_string.h:2147: note:                 std::basic_string<_CharT, _Traits, _Alloc>::basic_string() [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
test_push.C:61: error: conversion from char to non-scalar type std::string requested
test_push:90: error: no matching function for call to std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char&)’
/usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/bits/basic_string.tcc:220: note: candidates are: std::basic_string<_CharT, _Traits, _Alloc>::basic_string(typename _Alloc::rebind<_CharT>::other::size_type, _CharT, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
/usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/bits/basic_string.tcc:213: note:                 std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>] <near match>
/usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/bits/basic_string.tcc:206: note:                 std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, typename _Alloc::rebind<_CharT>::other::size_type, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]  

Plus d'info: Dans cette fonction, soit l'ensemble de la chaîne ou tout simplement la première lettre est poussé dans le vecteur en fonction de son contenu. Donc dans un sens, je suis à la recherche d'une fonction qui convertit un char dans un style C++ std::string (afin de la rendre acceptable pour un vecteur).

Merci,

Sriram

Quelles sont les erreurs que vous êtes réellement obtenir? Aussi, dans la seconde, êtes-vous essayer de copier str en main_string[0] ou vice-versa?
la chaîne est à son tour un vecteur de caractères. Pourquoi voulez-vous créer un vecteur avec de la ficelle ( mais un seul caractère)?
En fait votre problème, c'est de l'initialisation d'une chaîne de caractères avec un caractère.
ajout de plus d'info.
oui. c'est mon problème

OriginalL'auteur Sriram | 2011-03-16