invalid conversion from ‘int’ to ‘Array<int>**
Nov 17, 2015 at 4:08am UTC
I'm getting the above error pointing at my initlist in Matrix and I can't seem to figure out why. It definitely has something to do with the way I defined m. For some context, m is supposed to be an array of array pointers using Array.h.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#ifndef MATRIX_H
#define MATRIX_H
#include <iostream>
#include <cstdlib>
#include "Array.h"
using namespace std;
template <typename Element>
class Matrix
{
private :
int rows, cols;
Array<Element> **m; //typedef?
// Array<Array<Element> > *m; //maybe misplaced the pointer?
// Array<Array<Element> *> m; //still won't work
public :
Matrix( int newRows, int newCols )
: rows( newRows ), cols( newCols ), m( rows )
{
for (int i = 0; i < rows; i++ )
m[i] = new Array < Element >( cols );
}
};
#endif
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#ifndef ARRAY_H
#define ARRAY_H
#include <iostream>
#include <cstdlib>
#include <cassert>
using namespace std;
template <typename Type>
class Array
{
private :
int len;
Type * buf;
public :
Array( int newLen )
: len( newLen ), buf( new Type[newLen] ){}
};
#endif
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#include "Array.h"
#include "Matrix.h"
template < class T >
void fillMatrix( Matrix <T> & m )
{
int i, j;
for ( i = 0; i < m.numRows(); i++ )
m[i][0] = T();
for ( j = 0; j < m.numCols(); j++ )
m[0][j] = T();
for ( i = 1; i < m.numRows(); i++ )
for ( j = 1; j < m.numCols(); j++ )
{
m[i][j] = T(i * j);
}
}
int main()
{
Matrix < int > m(10,5);
fillMatrix( m );
cout << m;
}
Last edited on Nov 17, 2015 at 4:53am UTC
Nov 17, 2015 at 5:36am UTC
Anyone? I'm still spinning my wheels here seeing if it could be my array header or something syntactically wrong with the way I'm defining my template
Topic archived. No new replies allowed.