关于模板类调用模板类的参数问题(急)
代码如下:我主要定义了两个.h文件,Array2D.h和Row.h
Array2D.h
#ifndef _H_ARRAY2D
#define _H_ARRAY2D
#include "exceptionUser.h"
#include "Row.h"
#include "Array.h"
#include <stdexcept>
#include <exception>
//using namespace std;
template <typename T> class Row;
template <typename T>
class Array2D
{
public:
Array2D(unsigned int const row, unsigned int const column);
T& Select(unsigned int const row, unsigned int const column);
Row<T> operator [] (unsigned int const row);
unsigned int Row(void) const
{return m_row;}
unsigned int Column(void) const
{return m_column;}
protected:
unsigned int m_row;
unsigned int m_column;
Array<T> m_array;
};
/* implementation of the struture Array2D */
template <typename T>
Array2D<T>::Array2D(unsigned int const row, unsigned int const column)
: m_row(row), m_column(column), m_array(0, row*column)
{}
template <typename T>
T& Array2D<T>::Select(unsigned int const row, unsigned int const column)
{
if(row>=m_row || column>=m_column)
throw std::out_of_range("invalid row index or column index.");
return m_array[column + row * m_column];
}
template <typename T>
Row<T> Array2D<T>::operator [] (unsigned int const row)
{
if(row >= m_row)
throw std::out_of_range("invalid row index.");
return Row<T>(*this, row); //程序在这里出了问题,error C2275: 'T' : illegal use of this type as an expression
}
#endif
Row.h
#ifndef _H_ROW
#define _H_ROW
#include "Array2D.h"
template <typename T> class Array2D;
template <typename T>
class Row
{
public:
Row(Array2D<T> &array2D, unsigned int const row)
: m_array2D(array2D), m_row(row)
{}
T& operator [] (unsigned int column) const;
private:
Array2D<T> &m_array2D;
unsigned int const m_row;
};
/* implemenatation of the class Row */
template <typename T>
T& Row<T>::operator [] (unsigned int column) const
{
if(column >= m_array2D.Column())
throw std::out_of_range("invalid column index.");
return m_array2D.Select(m_row, column);
}
Array.h
#ifndef _H_ARRAY
#define _H_ARRAY
#include <iostream.h>
#include "excepArray.h"
template <class T> class Array;
template <class T>
ostream& operator << (ostream& os, Array<T> const &arr);
template <class T>
class Array
{
friend ostream& operator << (ostream& os, Array<T> const &arr);
protected:
T *m_data;
unsigned int m_base;
unsigned int m_length;
public:
Array();
Array(unsigned int base, unsigned int length = 0);
~Array();
Array(Array const &arr);
Array& operator = (Array const&);
T const & operator [] (unsigned int const index) const;
T& operator [] (unsigned int const index);
T const * Data() const;
unsigned int Base() const;
unsigned int Length() const;
void SetBase(unsigned int const newbase);
void SetLength(unsigned int const newlength);
};
//constructor functions
template <class T>
Array<T>::Array() :
data(NULL),
base(0),
length(0)
{}
template <class T>
Array<T>::Array(unsigned int base, unsigned int length) :
m_data(new T[length]),
m_base(base),
m_length(length)
{}
// copy constructor function
template <class T>
Array<T>::Array(Array<T> const &arr):
m_data(new T [arr.m_length]),
m_base(arr.m_base),
m_length(arr.m_length)
{
for (unsigned int i=0; i<arr.m_length; i++)
{
m_data[i] = arr.m_data[i];
}
}
//destructor function
template <class T>
Array<T>::~Array()
{
if(!m_base)
delete [] m_data;
m_base = NULL;
m_length = 0;
}
//assignment operator
//the method of writing the assignment operator
//very important
template <class T>
Array<T>& Array<T>::operator =(const Array<T> &arr)
{
if(this == &arr)
return *this;
// allocate the menmory and release the former resourses
T *pTemp = m_data;
m_data = new T [arr.m_length];
delete [] pTemp;
pTemp = NULL;
m_base = arr.m_base;
m_length = arr.m_length;
for(unsigned int i=0; i<m_length; i++)
{
m_data[i] = arr.m_data[i];
}
return *this;
}
//[] operator
template <class T>
T const& Array<T>::operator [](const unsigned int index) const
{
unsigned int const offset = index - m_base;
if(offset<0 || offset>=m_length)
{
throw excepArray()/*out_of_range ("invalid position")*/;
}
return m_data[offset];
}
template <class T>
T& Array<T>::operator [](const unsigned int index)
{
unsigned int const offset = index - m_base;
if(offset<0 || offset>=m_length)
{
throw excepArray()/*out_of_range ("invalid position")*/;
}
return m_data[offset];
}
// Get functions
template <class T>
unsigned int Array<T>::Base() const
{
return m_base;
}
template <class T>
T const* Array<T>::Data() const
{
return m_data;
}
template <class T>
unsigned int Array<T>::Length() const
{
return m_length;
}
//Set functions
template <class T>
void Array<T>::SetBase(unsigned int const newbase)
{
m_base = newbase;
}
template <class T>
void Array<T>::SetLength(unsigned int const newlength)
{
T* const newData = new T [newlength]; // const pointer pointing to variable
unsigned int const min =
m_length<newlength ? m_length : newlength;
for(unsigned int i=0; i<min; i++)
{
newData[i] = m_data[i];
}
delete [] m_data;
m_data = newData;
m_length = newlength;
}
//friend non-member function
template <class T>
ostream& operator << (ostream& os, Array<T> const &arr)
{
for(unsigned int i=0; i<arr.m_length; i++)
{
os << arr[i] << " ";
}
os << endl;
return os;
}
#endif
main函数是:
#include "stdafx.h"
#include "Array2D.h"
#include <stdexcept>
#include <exception>
void main(void)
{
int nRow = 0;
int nColumn = 0;
cout << "Pls input number of row: " << endl;
cin >> nRow;
cout << "Pls input number of column: " << endl;
cin >> nColumn;
Array2D<int> array2D(nRow, nColumn);
cout << "Pls input the data of the Array2D: " << endl;
for(int i=0; i<nRow; ++i)
for(int j=0; j<nColumn; ++j)
{
cin >> array2D[i][j];
}
cout << "The data of Array2D is: " << endl;
for(i=0; i<nRow; ++i)
{
for(int j=0; j<nColumn; ++j)
{
cout << array2D.Select(i, j) << " ";
}
cout << endl;
}
try
{
array2D.Select(8, 2);
}
catch(std::out_of_range e)
{
cout << "The exception is(Select): " << e.what() << endl;
}
try
{
array2D.Select(2, 8);
}
catch(std::out_of_range e2)
{
cout << "The exception is(Select): " << e2.what() << endl;
}
try
{
array2D[8][2];
}
catch(std::out_of_range e3)
{
cout << "The exception is([][]): " << e3.what() << endl;
}
try
{
array2D[2][9];
}
catch(std::out_of_range e4)
{
cout << "The exception is([][]): " << e4.what() << endl;
}
}
VC6编译错误为:
e:\data structure learning_080323\ds project\array2dtest\array2d.h(51) : error C2275: 'T' : illegal use of this type as an expression
e:\data structure learning_080323\ds project\array2dtest\array2d.h(15) : see declaration of 'T'
d:\program files\microsoft visual studio\vc98\include\xstring(133) : while compiling class-template member function 'class Row<int> __thiscall Array2D<int>::operator [](const unsigned int)'
Error executing cl.exe.
请各位大虾帮忙看一下。