I am trying to create a class called array whic is based on class vector. I use the template class for generalized data type and some constructors to reserve the memory for the array.
I ran into those error without any idea how to fix them. Please help me to explain the error and instruct me to fix them. Thanks
main.cpp: In function ‘int main(int, char**)’:
main.cpp:11:22: error: ‘class array<int>’ has no member named ‘size’
main.cpp:12:6: error: no match for ‘operator[]’ in ‘a[m]’
main.cpp:13:22: error: no match for ‘operator[]’ in ‘a[m]’
Compilation failed.
----------------------------------------------------------------------
Code:
#ifndef ARRAY_H
#define ARRAY_H
#include <string> //for std::string
#include <vector> //for std::vector
template <typename T>
class array
{
private:
size_t N1,N2,N3;
std::vector<T> array1D;
std::vector<std::vector<T> > array2D;
std::vector<std::vector<std::vector<T> > > array3D;
public:
// default constructor
array();
array(size_t);
array(size_t,size_t);
array(size_t,size_t,size_t);
};
#endif
--------------------------------------------------------------
# include "array.h"
template <typename T>
array<T>::array(size_t i)
{
N1 = i;
// make room for some elements
array1D.reserve(N1);
}
template <typename T>
array<T>::array(size_t i,size_t j)
{
N1=i;
N2=j;
array2D.reserve(N1);
for (int m=0; m < array2D.size(); m++)
array2D[m].reserve(N2);
}
-------------------------------------------------------
#include <iostream> //for std::cout
#include <vector> //for std::vector
#include "array.h"
int main(int argc, char * argv[])
{
size_t i=10;
array<int> a(i);
for (int m=0; m < a.size(); m++) {
a[m]=m;
std::cout << a[m] << '\n';
}
return 0;
}