Question

I am getting a strange c++ error:

main.cpp:81:9: error: request for member ‘push_back’ in ‘points’, which is of non-class type ‘std::vector<std::vector<float> >()’

I am reading this like c++ is trying to tell me that the function push_back is not part of the vector class. Here is my relevant code:

        vector<vector<float> > points(); //construct an empy vector of vectors                                          
        vector<float> first(3,0);        //construct 0 vector in R^3                                                     
        points.push_back(first);         //put (0, 0, 0) in points
  1. Where is my error here and why does c++ not like this? This seems very intuitive to me.

All help is greatly appreciated!

Était-ce utile?

La solution

This is a case of MVP (Most Vexing Parse):

vector<vector<float> > points();

This is actually a function declaration, change it to:

vector<vector<float> > points;

With the first version, C++ thinks you're declaring a function called points which takes no arguments and returns a vector of vector of floats. That's why it's complaining about it being a non-class type.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top