no matching function for call to 'std::vector<double, std::allocator<double> >::push_back(double [2])

When I try to compile the following code I got the above error message.
My code is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//Data.h

class Data {
public:
    static Data read_from_file(string filename); 
    Data(vector<double> datum, double x, double y);
    Data(const Data& datum);
    double x() const { return _x; }
    double y() const ( return _y; }
    vector <double> datum() { return _datum; }
virtual ~Data () {}
private:
    double _x, _y;
    vector <double> _datum; 

}


1
2
3
4
5
6
7
8
9
10
11
12
13
//Data.cpp

Data::Data (vector<double> datum, double x, double y):
            _datum (datum), _x (x), _y (y) {}
Data::Data (const Data& datum):
           _x (datum._x), _y (datum._y) {}

Data Data::read_from_file(string filename) {
 \\getline etc. to get x and y
      double point[] = {x, y};
      datum = datum.push_back(point); 
}


The error is at the line datum = datum.push_back(point)
"no matching function for call to 'std::vector<double, std::allocator<double> >::push_back(double [2])'"

Could someone tell me what I am missing?
closed account (zb0S216C)
Your vector handles ordinary double types, not double* types. When you tried to pass an array, the compiler started to cry because arrays can only be passed to pointers. Non-pointer types cannot accept arrays. Convert your array to: std::vector< double * > Vector.

I've just noticed something. Since arrays cannot be passed like this: push_back( double[2] ), you'll have to use new; I don't condone it, however, but it's an option. Ne555's solution works best.

Wazzak
Last edited on
But then you will be storing the address of a temporary.
If you want a vector of points, then make a vector of points.
1
2
3
4
5
struct point_2d{
  double x[2];
};

vector<point_2d> datum_;
Ne555, I don't quite understand your answer yet. If I have the vector of points like you wrote, is push_back allowed then?
Topic archived. No new replies allowed.