c++variablesvector

Adding a variable to a vector in c++


I have a std::vector called possible_numbers and I am trying to use its .push_back() method to add the value of the variable i to the back of the vector. When I try to do it, my code ends up adding i to the vector, instead of the value of i.

#include <iostream>
#include <vector>

int main() {
    std::vector<std::string> possible_numbers;
    for(int i = 1; i < 101; i++) {
        possible_numbers.push_back("i");
    }
}

I think that it might be the quotation marks around i. However, when I try removing them, I get quite a long error from my compiler written below:

guess_number.cpp: In function 'int main()':
guess_number.cpp:7:33: error: no matching function for call to 'std::vector<std::__cxx11::basic_string<char> >::push_back(int&)'
    7 |     possible_numbers.push_back(i);
      |                                 ^
In file included from c:\mingw\lib\gcc\mingw32\9.2.0\include\c++\vector:67,
                 from guess_number.cpp:2:
c:\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_vector.h:1184:7: note: candidate: 'void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = std::__cxx11::basic_string<char>; _Alloc = std::allocator<std::__cxx11::basic_string<char> >; std::vector<_Tp, _Alloc>::value_type = std::__cxx11::basic_string<char>]'
 1184 |       push_back(const value_type& __x)
      |       ^~~~~~~~~
PS (my file's directory)> g++ guess_number.cpp
PS (My file's directory> g++ guess_number.cpp
guess_number.cpp: In function 'int main()':
guess_number.cpp:7:33: error: no matching function for call to 'std::vector<std::__cxx11::basic_string<char> >::push_back(int&)'
    7 |     possible_numbers.push_back(i);
      |                                 ^
In file included from c:\mingw\lib\gcc\mingw32\9.2.0\include\c++\vector:67,
                 from guess_number.cpp:2:
c:\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_vector.h:1184:7: note: candidate: 'void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = std::__cxx11::basic_string<char>; _Alloc = std::allocator<std::__cxx11::basic_string<char> >; std::vector<_Tp, _Alloc>::value_type = std::__cxx11::basic_string<char>]'
 1184 |       push_back(const value_type& __x)
      |       ^~~~~~~~~
c:\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_vector.h:1184:35: note:   no known conversion for argument 1 from 'int' to 'const value_type&' {aka 'const std::__cxx11::basic_string<char>&'}
 1184 |       push_back(const value_type& __x)
      |                 ~~~~~~~~~~~~~~~~~~^~~
c:\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_vector.h:1200:7: note: candidate: 'void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = std::__cxx11::basic_string<char>; _Alloc = std::allocator<std::__cxx11::basic_string<char> >; std::vector<_Tp, _Alloc>::value_type = std::__cxx11::basic_string<char>]'       
 1200 |       push_back(value_type&& __x)
      |       ^~~~~~~~~
c:\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_vector.h:1200:30: note:   no known conversion for argument 1 from 'int' to 'std::vector<std::__cxx11::basic_string<char> >::value_type&&' {aka 'std::__cxx11::basic_string<char>&&'}
 1200 |       push_back(value_type&& __x)
      |                 ~~~~~~~~~~~~~^~~

Can anybody please help?


Solution

  • There are multiple issues with this code snippet:

    1. If you are trying to generate a list of numbers, why are you using std::vector<std::string>? This is creating a vector of strings, if you are looking for ints, use std::vector<int>.

    2. Pushing "i" is pushing the string literal "i", not the variable i. When removing the "s, you are pushing an int into a vector expecting string, that's why you get an error for no overload for a push_back() with argument int for a vector of strings. Another one of the errors is the compiler failing to convert "i" from char to int.

    What you are likely looking for is:

    int main() {
       std::vector<int> possible_numbers;
       for (int i = 1; i < 101; i++) {
          possible_numbers.push_back(i);
       }
    }