我正在尝试开发一个由 Row、Col 参数化的矩阵类模板,并在此处键入...接口(interface)
template < std::size_t N, std::size_t M , typename data_type = double>
class Matrix {
public:
template<std::size_t R, std::size_t C, typename T>
friend std::ostream& operator<< (std::ostream& ,const Matrix<R,C,T>& ) ;
template<std::size_t R, std::size_t C, typename T>
friend std::ostream& operator<< (std::ostream& ,Matrix<R,C,T>& ) ;
public:
constexpr Matrix() noexcept ;
constexpr explicit Matrix(const data_type val) noexcept ;
template <typename ...T>
constexpr Matrix(T&& ...args) noexcept;
Matrix<N,M,data_type>(const Matrix<N,M,data_type>& rsh) noexcept;
Matrix<N,M,data_type>(Matrix<N,M,data_type>& rsh) noexcept;
decltype(auto) type() const noexcept;
decltype(auto) print() const noexcept ;
data_type& operator()(std::size_t row, std::size_t col) noexcept ;
const data_type operator()(std::size_t row, std::size_t col)const noexcept ;
Matrix& operator=(const Matrix& rhs) noexcept ;
private:
std::valarray<data_type> data ;
decltype(auto) getSize() const noexcept { return data.size(); }
};
问题是复制构造函数(定义如下)不起作用..从某种意义上说,当我尝试构造一个 Matrix 对象时
Matrix<3,3,double> M4(M3);
其中M3当然是Matrix<3,3,double>类型 我遇到了很多错误,因为编译器尝试调用运算符重载 () ,或者如果 ai 尝试注释掉该运算符将调用 Matrix(T&&...args) ! ...我编写了我的复制构造函数如下:
template <std::size_t R, std::size_t C, typename data_type>
Matrix<R,C,data_type>::Matrix(const Matrix<R,C,data_type>& rhs) noexcept {
std::cout << "copy Constructor" << std::endl;
*this = rhs ; // I have defined the assignament operator and it works!
}
template <std::size_t R, std::size_t C, typename data_type>
Matrix<R,C,data_type>::Matrix(Matrix<R,C,data_type>& rhs) noexcept {
std::cout << "copy Constructor" << std::endl;
*this = rhs ; // I have defined the assignament operator and it works!
}
我知道没有必要定义 2 个复制构造函数...但我尝试过只使用第一个,但事情没有改变..我总是得到:
g++ mainMatrix.cpp --std=c++1y
mainMatrix.cpp: In function ‘int main()’:
mainMatrix.cpp:31:8: error: no match for call to ‘(Matrix<3ul, 3ul, double>) (Matrix<3ul, 3ul, double>&)’
M4(M2);
^
这里是类成员定义:
template<std::size_t R, std::size_t C, typename T>
inline constexpr Matrix<R,C,T>::Matrix() noexcept {
data.resize(R*C);
std::size_t k=0 ;
for(auto& i : data){
data[k] = 0.0 ;
k += 1;
}
}
template<std::size_t R, std::size_t C, typename T>
inline constexpr Matrix<R,C,T>::Matrix(const T val) noexcept {
data.resize(R*C);
std::size_t k=0 ;
for (auto& i : data ){
data[k] = val ;
k += 1;
}
}
template <std::size_t R, std::size_t C, typename data_type>
template <typename ...T>
inline constexpr Matrix<R,C,data_type>::Matrix(T&& ...args) noexcept : data{args ... }
{
static_assert(sizeof...(T) == R*C, "Data list number doesn't fit the Matrix size!" );
if(sizeof...(T) != R*C)
std::cerr << "Data list number doesn't fit the Matrix size!!" << std::endl ;
}
template <std::size_t R, std::size_t C, typename data_type>
Matrix<R,C,data_type>::Matrix(const Matrix<R,C,data_type>& rhs) noexcept {
std::cout << "copy Constructor" << std::endl;
*this = rhs ;
}
template <std::size_t R, std::size_t C, typename data_type>
Matrix<R,C,data_type>::Matrix(Matrix<R,C,data_type>& rhs) noexcept {
std::cout << "copy Constructor" << std::endl;
*this = rhs ;
}
template <std::size_t R, std::size_t C, typename data_type>
decltype(auto) Matrix<R,C,data_type>::print() const noexcept {
unsigned short k = 0;
for(unsigned short i=0; i < R ; i++ ){
for (unsigned short j=0; j < C ; j++){
std::cout << std::setw(6) << data[k] << ' ' ;
k+=1;
}
std::cout << std::endl ;
}
}
template <std::size_t R, std::size_t C, typename data_type>
decltype(auto) Matrix<R,C,data_type>::type() const noexcept
{
std::cout << "Matrix data type " << type_id_with_cvr<decltype(*(std::begin(data)))>().pretty_name()
<< std::endl;
}
template <std::size_t R, std::size_t C, typename data_type>
inline data_type& Matrix<R,C,data_type>::operator()(std::size_t row, std::size_t col) noexcept {
assert( row <= R && col <= C);
return data[ (row-1)*C + (col-1) ];
}
template <std::size_t R, std::size_t C, typename data_type>
inline const data_type Matrix<R,C,data_type>::operator()(std::size_t row, std::size_t col) const noexcept {
static_assert( row <= R && col <= C, "Error occurred in operator(i,j) index out of bounds");
return data[ (row-1)*C + (col-1) ];
}
template <std::size_t R, std::size_t C, typename data_type>
inline Matrix<R,C,data_type>& Matrix<R,C,data_type>::operator=(const Matrix& rhs) noexcept {
std::cout << "Copy Assignament operator " << std::endl;
if( this == &rhs ){
std::cerr << "Self assignament occurred in assignament operator! " << std::endl;
exit(1);
}
else
{
this->data.resize( rhs.data.size() );
this->data = rhs.data ;
}
}
template <std::size_t N, std::size_t M, typename data_type>
std::ostream& operator<< (std::ostream& output, const Matrix<N,M,data_type>& rhs){
rhs.print();
return output;
}
template <std::size_t N, std::size_t M, typename data_type>
std::ostream& operator<< (std::ostream& output, Matrix<N,M,data_type>& rhs){
rhs.print();
return output;
}
以及用于测试类的main
# include "Matrix.H"
# include <iostream>
using namespace std;
int main(){
Matrix<3,3,double> M1(1.11);
Matrix<3,3,double> M2(1.121,2.323,3.545,4.544,5.737,6.990,7.09,8.12,9.09);
cout << M2(1,1) << ' ' << M2(1,2) << ' ' << M2(1,3) << endl;
cout << M2(2,1) << ' ' << M2(2,2) << ' ' << M2(2,3) << endl;
cout << M2(3,1) << ' ' << M2(3,2) << ' ' << M2(3,3) << endl;
M1.print();
M2.print();
cout << M2 << endl;
M2.type();
Matrix<3,3,double> M3;
Matrix<3,3,double> M4;
M4(M2);
M3 = M2 ;
M3.print();
M4.print();
return 0;
}
有人可以帮助我吗?提前致谢
如果我尝试调用Matrix<3,3,double> M4(M2);
我得到了这个:
g++ mainMatrix.cpp
In file included from mainMatrix.cpp:2:0:
Matrix.H:189:87: error: no ‘data_type& Matrix<N, M, data_type>::operator()(std::size_t, std::size_t)’ member function declared in class ‘Matrix<N, M, data_type>’
inline data_type& Matrix<R,C,data_type>::operator()(std::size_t row, std::size_t col) noexcept {
^~~~~~~~
Matrix.H:199:98: error: no ‘const data_type Matrix<N, M, data_type>::operator()(std::size_t, std::size_t) const’ member function declared in class ‘Matrix<N, M, data_type>’
const data_type Matrix<R,C,data_type>::operator()(std::size_t row, std::size_t col) const noexcept {
最佳答案
in the sense that when I try to construct a Matrix object as
Matrix<3,3,double> M4(M3);
你不知道。您的主要功能包含以下行
Matrix<3,3,double> M4;
M4(M2);
这不是对复制构造函数的调用,而是对 M4 的(未定义)运算符 () 的调用,因此会出现错误。
关于c++ - 矩阵模板类复制构造函数不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46926742/