You can go either way as I stated in the comment. Without the ampersand, you create a copy. That's the difference. Actually, if you use const T, use const T&, but if you return a copy, just make it T. My bad in there too. |
What are the advantages/disadvantages of choosing one way over the other?
Here is my code so far (I added another function that prints out the entire matrix, disregard that. Also, the while loop is just something I added to keep the program running so I could test it without having to close and reopen it):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
|
#include<typeinfo>
#include<limits>
#include<cstdlib>
#include<iostream>
template<class T>
class matrix2D {
int i,j,NROWS,NCOLS;
public:
T**element;
void printout();
T& operator[](int,int);
const T& operator[](int,int) const;
matrix2D(int,int);
~matrix2D();
};
template <class T>
matrix2D<T>::matrix2D(int a,int b)
{
NROWS=a;
NCOLS=b;
try
{
element=new T*[NROWS];
for(i=0;i<NROWS;i++)
element[i]=new T[NCOLS];
}
catch(std::bad_alloc&)
{
std::cout<<std::endl<<std::endl<<"Error allocating memory"<<std::endl<<std::endl<<std::cout<<"Press ENTER to continue...";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
exit(1);
}
if(typeid(T)==typeid(char))
{
for(i=0;i<NROWS;i++)
{
for(j=0;j<NCOLS;j++)
element[i][j]='0';
}
}
else
for(i=0;i<NROWS;i++)
{
for(j=0;j<NCOLS;j++)
element[i][j]=0;
}
}
template <class T>
matrix2D<T>::~matrix2D()
{
for(i=0;i<NROWS;i++)
delete [] element[i];
delete [] element;
}
template <class T>
void matrix2D<T>::printout()
{
for(i=0;i<NROWS;i++)
{
for(j=0;j<NCOLS;j++)
std::cout<<element[i][j]<<" ";
std::cout<<std::endl;
}
}
template <class T>
T& matrix2D<T>::operator[](int row,int col)
{
return element[row][col];
}
template <class T>
const T& matrix2D<T>::operator[](int row,int col) const
{
return element[row][col];
}
int main()
{
int x,y;
while(true)
{
std::cout<<"Enter number of rows: ";
std::cin>>x;
std::cout<<std::endl<<"Enter number of columns: ";
std::cin>>y;
matrix2D<char>A(x,y);
A[0,0]='a';
std::cout<<std::endl<<A[0,0]<<std::endl<<std::endl;
A.printout();
std::cout<<std::endl<<std::endl;
}
std::cout<<"Press ENTER to continue...";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
return 0;
}
|
I am getting the following errors now:
C:\Users\Chris\Documents\Untitled1.cpp|12|error: 'T& matrix2D<T>::operator[](int, int)' must take exactly one argument|
C:\Users\Chris\Documents\Untitled1.cpp|13|error: 'const T& matrix2D<T>::operator[](int, int) const' must take exactly one argument|
C:\Users\Chris\Documents\Untitled1.cpp|71|error: 'T& matrix2D<T>::operator[](int, int)' must take exactly one argument|
C:\Users\Chris\Documents\Untitled1.cpp|77|error: 'const T& matrix2D<T>::operator[](int, int) const' must take exactly one argument|
C:\Users\Chris\Documents\Untitled1.cpp||In function 'int main()':|
C:\Users\Chris\Documents\Untitled1.cpp|92|error: no match for 'operator[]' in 'A[(0, 0)]'|
C:\Users\Chris\Documents\Untitled1.cpp|93|error: no match for 'operator[]' in 'A[(0, 0)]'|
||=== Build finished: 6 errors, 0 warnings ===|
Again, thank you for taking the time to answer my many questions. If you have any suggestions for how I could improve other parts of my code, please say. I wasn't sure what was the best way to go for the initialization part of the constructor. I use typeid to perform different initializations based on the type that is passed to the class, but should I instead create function template specializations? function overloads? class template specialization?