[英]How do in include a non-member operator- overloading for a template class in c++?
I am new to c++ and templates is definitely not friendly in syntax. 我是C ++的新手,模板在语法上绝对不友好。 Basically, here are some of the functions i've written, tested, and finished.
基本上,这是我编写,测试和完成的一些功能。 Just one quick question, i've been trying for hours to write a non-member operator- overload for my matrix class and have been getting all types of syntax errors.
只是一个简单的问题,我一直在试图为矩阵类编写一个非成员运算符重载,并且已经遇到了所有类型的语法错误。 So, where do I start?
那么,我从哪里开始呢?
Here is my myMatrix.h: 这是我的myMatrix.h:
#ifndef MYMATRIX_H
#define MYMATRIX_H
#include <exception>
#include <vector>
#include <iostream>
using namespace std;
/* I'm using this matrix to store data */
template<typename T>
using twoD = std::vector<std::vector<T>>;
template<class T>
class Matrix{
private:
int rows;
int cols;
twoD<T> matrix;
protected:
void validSizeCheck(int rows, int cols);
public:
Matrix(int rows, int cols);
Matrix(int rows, int cols, twoD<T> newMatrix);
twoD<T> getMatrix();
int getRows();
int getCols();
void printMatrix();
void operator=(const Matrix<T> &);
Matrix<T> &operator+=(const Matrix<T> &);
Matrix<T> operator+(const Matrix<T> &);
Matrix<T> operator*(const Matrix<T> &);
Matrix<T> operator*(const T &);
};
Here is one of the thing I tried: 这是我尝试过的事情之一:
template <class T>
Matrix<T> operator-(const Matrix<T>& lhs,const Matrix<T>& rhs){
if(lhs.rows != rhs.cols || lhs.rows != rhs.cols{
throw exception();
}
Matrix tmp(lhs.rows, lhs.cols);
for(int i = 0; i < lhs.rows; i++){
for(int j = 0; j < cols; j++){
tmp.matrix[i][j] = lhs.matrix[i][j]+rhs.matrix[i][j];
}
}
return tmp;
}
Obviously that didn't work.. So what are the syntax that I should be following for this? 显然那是行不通的。.那么我应该遵循的语法是什么? Also, this might be a very dumb question but should the non-member function be in the header file or the .cpp file?
另外,这可能是一个非常愚蠢的问题,但是非成员函数应该在头文件还是.cpp文件中? So far, everything that I wrote has been in the .h file as they're all templates..
到目前为止,我编写的所有内容都在.h文件中,因为它们都是模板。
Any help would be greatly appreciated. 任何帮助将不胜感激。
UPDATED: I finally got the code to works, for class & function declaration, I added this: 更新:我终于使代码工作了,对于类和函数的声明,我添加了以下代码:
template<class T>
class Matrix{
private:
...
protected:
...
public:
...
template<class U>
friend Matrix<U> operator-(const Matrix<U>& lhs, const Matrix<U>& rhs);
};
Here is the function definition: 这是函数定义:
template <class U>
Matrix<U> operator-(const Matrix<U>& lhs,const Matrix<U>& rhs){
if(lhs.rows != rhs.cols || lhs.rows != rhs.cols){
throw exception();
}
Matrix<U> tmp(lhs.rows, lhs.cols);
for(int i = 0; i < lhs.rows; i++){
for(int j = 0; j < lhs.cols; j++){
tmp.matrix[i][j] = lhs.matrix[i][j]-rhs.matrix[i][j];
}
}
return tmp;
}
works perfect, no warning! 完美的作品,没有警告! Thanks for all the helps
感谢所有帮助
Members rows
, cols
are private. 成员
rows
, cols
私有。 Non-class member operator must be a friend of a class. 非班级成员运算符必须是班级的朋友。 Declare it in the class:
在课堂上声明:
friend Matrix<T> operator-<>(const Matrix<T>& lhs,const Matrix<T>& rhs);
Read this article for more info: Operator overloading : member function vs. non-member function? 阅读本文以获得更多信息: 运算符重载:成员函数与非成员函数?
Also, this might be a very dumb question, but should the non-member function be in the header file or the .cpp file?
另外,这可能是一个非常愚蠢的问题,但是非成员函数应该在头文件还是.cpp文件中? So far, everything that I wrote has been in the .h file as they're all templates.
到目前为止,我编写的所有内容都在.h文件中,因为它们都是模板。
You did it right, all templates must be in a header file. 您做对了,所有模板都必须在头文件中。 Here is more info: Why can templates only be implemented in the header file?
这是更多信息: 为什么只能在头文件中实现模板?
You need to use the template parameters again in the declaration of the temp variable inside: 您需要在里面的temp变量的声明中再次使用模板参数:
template <class T>
Matrix<T> operator-(const Matrix<T>& lhs,const Matrix<T>& rhs){
if(lhs.rows != rhs.cols || lhs.rows != rhs.cols{
throw exception();
}
Matrix<T> tmp(lhs.rows, lhs.cols);
for(int i = 0; i < lhs.rows; i++){
for(int j = 0; j < cols; j++){
tmp.matrix[i][j] = lhs.matrix[i][j]+rhs.matrix[i][j];
}
}
return tmp;
}
(change is in Line 5: Matrix<T> tmp(lhs.rows, lhs.cols);
) (更改在第5行:
Matrix<T> tmp(lhs.rows, lhs.cols);
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.