Invalid Initialization Error

Soo who can help me with this...I'm still learning the in's and outs of OOP, mostly methods and how to declare them. Here's the error:


In file included from main.cpp:2:0:
Mat.h: In member function ‘D& Mat<D>::operator()(int, int) const [with D = double]’:
Mat.h:100:13: instantiated from ‘std::ostream& operator<<(std::ostream&, const Mat<D>&) [with D = double, std::ostream = std::basic_ostream<char>]’
main.cpp:9:18: instantiated from here
Mat.h:141:37: error: invalid initialization of reference of type ‘double&’ from expression of type ‘int’


And here's the code it's yelling at me for:

1
2
3
4
5
6
7
template< class D >
D & Mat<D>::operator()(const int row_index, const int col_index) const
{
    if (row_index >= _rowsize || col_index >= _colsize) throw IndexError();

    return (_p[row_index][col_index]);
}


Any help would be much appreciated. Thanks
Mat.h:141:37: error: invalid initialization of reference of type ‘double&’ from expression of type ‘int’


The error is pretty self explanitory. You're trying to convert an int to a double&. This is illegal.

The function returns a D& (and in this template, D=double), but _p is an array of ints. _p should probably be an array of D's, not ints
Last edited on
Ok. I don't know why I didn't bother to look at that. I think I assumed that the array was D(= double) and I didn't even check. Alright, thanks. Fixed now
Topic archived. No new replies allowed.