I'm using Cygwin with GCC, and ultimately I want to read in a file of characters into a vector of characters, and using this code
#include <fstream> #include <vector> #include <stdlib.h> using namespace std; int main (int argc, char *argv[] ) { vector<char> string1(); string1.push_back('a'); return 0; }
generates this compile time error:
main.cpp: In function
int main(int, char**)': main.cpp:46: error: request for member
push_back' instring1', which is of non -class type
std::vector > ()()'
I tried this with a vector of ints and strings as well and they had the same problem.
C++ Vector Library - push_back() Function The C++ function std::vector::push_back() inserts new element at the end of vector and increases size of vector by one.
push_back method() in C++ is a method that is part of the vector as a data structure in C++. It is used for pushing elements from the back of the vector.
Yes, std::vector<T>::push_back() creates a copy of the argument and stores it in the vector.
Don't use parentheses to invoke the default constructor:
vector<char> string1;
Otherwise this declares a function string1
that takes no argumentes and returns a vector<char>
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With