Oli*_*nde 3 c++ templates operator-overloading
我在编译以下标题时遇到问题.这是我第一次使用模板,我想我得错了.编译器指出错误vector<vector<T>> data_;和操作符重载函数.我希望data_矢量与OptBaseMatrix对象具有相同的类型,但我不知道该怎么做...我真的不知道如何解决这个问题.救命!
#ifndef OPTBASEMATRIX_H
#define OPTBASEMATRIX_H
#include <vector>
template<typename T>
class OptBaseMatrix
{
public:
vector<vector<T>> data_;
OptBaseMatrix(int rows, int cols);
~OptBaseMatrix();
void readMatrix();
void printMatrix();
int getRows();
int getCols();
OptBaseMatrix<T> operator+(const OptBaseMatrix<T>& matrix1, const OptBaseMatrix<T>& matrix2);
private:
int rows_;
int cols_;
};
#endif // OPTBASEMATRIX_H
Run Code Online (Sandbox Code Playgroud)
更新:这是调试器日志的一个片段:
Error 1 error C2143: syntax error : missing ';' before '<' optbasematrix.h 17 TD2
Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int optbasematrix.h 17 TD2
Run Code Online (Sandbox Code Playgroud)
我试过修改vector> data_; 通过vector> data_; 并且仍然得到相同的错误:/我在某处读到我的模板类头(.h)和实现(.cpp)必须在同一个文件中......这可能是相关的吗?
更新2:哇!我忘了"使用namespace std;".问题现在似乎已经解决了!
你需要在两者之间留一个空格>.
vector<vector<T> > data_;
Run Code Online (Sandbox Code Playgroud)
没有空间,>>被视为流提取/右移操作符.
此外,您需要声明operator+为自由函数,或者必须仅使用一个参数声明它:
// Member function
Matrix<T> operator+(const Matrix<T>& other) const;
// Free function (`friend` makes the function free
// even though it's declared within the scope of the class definition)
friend Matrix<T> operator+(const Matrix<T>& lhs, const Matrix<T>& rhs);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
168 次 |
| 最近记录: |