Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ vector.push_back error: request for member 'push_back'..., which is of non-class type 'vector(char, allocator(char)) ()()'

Tags:

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 memberpush_back' in string1', which is of non -class typestd::vector > ()()'

I tried this with a vector of ints and strings as well and they had the same problem.

like image 459
Dlongnecker Avatar asked May 05 '10 03:05

Dlongnecker


People also ask

What does vector Push_back do in C++?

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.

What does Push_back in C++ mean?

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.

Does std::vector Push_back make a copy?

Yes, std::vector<T>::push_back() creates a copy of the argument and stores it in the vector.


1 Answers

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>.

like image 134
Georg Fritzsche Avatar answered Nov 11 '22 02:11

Georg Fritzsche