我必须制作一个使用矩阵的学校项目。在 Visual Studio 2010 中,一切正常。测试服务器具有“matrix_base”和主要功能,例如:
template<class T, int N, int M, class LeftOp, class RightOp> class my_matrixAdd;
template<class T, int N, int M, class LeftOp, class RightOp> class my_matrixSub;
template<class T, int N, int M, class LeftOp, class RightOp> class my_matrixMul;
template<class T, int N, int M>
class my_matrix //: matrix_base
{
private:
T arrData[N][M];
int columns;
int rows;
public:
my_matrix() : columns(N), rows(M) {}
template<class LeftOp, class RightOp>
my_matrix<T, N, M> & operator=(
const my_matrixAdd<T, N, M, LeftOp, RightOp> & mx)
{
for(int i=0; i<N; ++i)
for(int j=0; j<M; ++j)
{
At(i, j) = mx.At(i, j);
}
return *this;
}
template<class LeftOp, class RightOp>
my_matrix<T, N, M> & operator=(
const my_matrixMul<T, N, M, LeftOp, RightOp> & mx)
{
for(int i=0; i<N; ++i)
for(int j=0; j<M; ++j)
{
At(i, j) = mx.At(i, j);
}
return *this;
}
template<class LeftOp, class RightOp>
my_matrix<T, N, M> & operator=(
const my_matrixSub<T, N, M, LeftOp, RightOp> & mx)
{
for(int i=0; i<N; ++i)
for(int j=0; j<M; ++j)
{
At(i, j) = mx.At(i, j);
}
return *this;
}
virtual ~my_matrix() {}
const T& At(int n, int m) const
{
return arrData[n][m];
}
T& At(int n, int m)
{
return arrData[n][m];
}
const T& operator()(int n, int m) const
{
return arrData[n][m];
};
T& operator()(int n, int m)
{
return arrData[n][m];
};
int Columns() const { return columns; }
int Rows() const { return rows; }
};
template< class T, int N, int M, class LeftOp, class RightOp>
class my_matrixAdd
{
private:
const LeftOp& m_lhs;
const RightOp& m_rhs;
public:
my_matrixAdd(
const LeftOp& lhs, const RightOp& rhs) :
m_lhs(lhs), m_rhs(rhs) {}
T At(int x, int y) const
{
return m_lhs.At(x, y) + m_rhs.At(x, y);
}
};
template<class T, int N, int M>
inline my_matrixAdd<T, N, M, my_matrix<T, N, M> , my_matrix<T, N, M> >
operator+(const my_matrix<T, N, M> &l, const my_matrix<T, N, M> &r)
{
return my_matrixAdd<T, N, M, my_matrix<T, N, M> ,
my_matrix<T, N, M> > (l, r);
}
template<class T, int N, int M, class LeftOp, class RightOp>
inline my_matrixAdd<T, N, M, my_matrixAdd<T, N, M, LeftOp, RightOp> ,
my_matrix<T, N, M> >
operator+(const my_matrixAdd<T, N, M, LeftOp, RightOp> &l,
const my_matrix<T, N, M> &r)
{
return my_matrixAdd<T, N, M, my_matrixAdd<T, N, M, LeftOp, RightOp> ,
my_matrix<T, N, M> > (l, r);
}
template<class T, int N, int M, class LeftOp, class RightOp>
inline my_matrixAdd<T, N, M, my_matrixSub<T, N, M, LeftOp, RightOp> ,
my_matrix<T, N, M> >
operator+(const my_matrixSub<T, N, M, LeftOp, RightOp> &l,
const my_matrix<T, N, M> &r)
{
return my_matrixAdd<T, N, M, my_matrixSub<T, N, M, LeftOp, RightOp> ,
my_matrix<T, N, M> > (l, r);
}
int main()
{
my_matrix<int,2,2> mtx1;
my_matrix<int,2,2> mtx2;
my_matrix<int,2,2> mtx3;
my_matrix<int,2,2> mtx4;
mtx4 = mtx1 + mtx2 + mtx3;
return 0;
}
服务器中的编译器是带有 -static -O2 参数的 g++。我得到如下错误:
/var/www/F/I704e/3/1/teszt1.cpp:在函数'int main()'中:
/var/www/F/I704e/3/1/teszt1.cpp:42: 错误: 'operator+(const my_matrix&, const my_matrixAdd&) 中的 'operator+' 不匹配 [with T = int, int N = 5, int M = 3, LeftOp = my_matrix, RightOp = my_matrix](((const my_matrixAdd, my_matrix >&)((const my_matrixAdd, my_matrix >*)(& operator+(const my_matrix&, const my_matrix&) [with T = int, int N = 5 , int M = 3](((const my_matrix&)((const my_matrix*)(& mtx3)))))))) + operator+(const my_matrixAdd&, const my_matrix&) [with T = int, int N = 5, int M = 3, LeftOp = my_matrix, RightOp = my_matrix](((const my_matrix&)((const my_matrix*)(& mtx6))))'</p>
就像:
在 /var/www/F/I704e/3/1/teszt1.cpp:6 包含的文件中:
/var/www/Hallg/I704e/3/h145172/7/feladat.cpp:在函数 'my_matrixAdd, my_matrixAdd > operator+(const my_matrixAdd&, const my_matrixAdd&) [with T = int, int N = 5, int M = 3, LeftOp = my_matrix,RightOp = my_matrix]':
请帮我!谢谢你!