英文:
Assignment operator overloading for templated matrix class
问题
I am implementing assignment operator function for a template matrix class.
It should take care of different datatype matrix assignments. eg Integer matrix is assigned to a double matrix.
for that i have following declaration:
template
My problem is, if i implement the function within class declaration, it works. But if i implement it outside the class declaration like,
template<class T, class U> MyMatrix
i get following error:
error: no declaration matches 'MyMatrix
What am i doing wrong?
英文:
I am implementing assignment operator function for a template matrix class.
It should take care of different datatype matrix assignments. eg Integer matrix is assigned to a double matrix.
for that i have following declaration:
template<class U> MyMatrix<T>& operator= (const MyMatrix<U> &rhs) {...}
My problem is, if i implement the fuction within class declaration, it works. But if i implement it outside the class declaration like,
template<class T, class U> MyMatrix<T>& MyMatrix<T>::operator= (const MyMatrix<U> &rhs){...}
i get following error:
error: no declaration matches ‘MyMatrix<T>& MyMatrix<T>::operator=(const MyMatrix<U>&)’
What am i doing wrong?
答案1
得分: 1
当提供类成员模板的课外定义时,您必须提供与类模板以及成员模板对应的模板参数子句,如下所示:
template<class T> // 类模板的参数子句
template<class U> // 成员模板的参数子句
MyMatrix<T>& MyMatrix<T>::operator= (const MyMatrix<U>& rhs)
{ // 在此处添加代码
}
英文:
When providing out of class definition of a member template you've to provide the template parameter clause correspondimg to both the class template as well as the member template as shown below:
template<class T> // parameter clause for class template
template<class U> // parameter clause for member template
MyMatrix<T>& MyMatrix<T>::operator= (const MyMatrix<U> &rhs)
{ // code here
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论