我做了这个代码运营商<<重载在C++中
声明:
template <class T>
class Matrix
{
std::vector< std::vector<T> > mat;
size_t rows , cols;
public:
Matrix<T>();
Matrix<T>(const std::string);
Matrix(const size_t r, const size_t c, const std::vector<T> v);
Matrix<T> operator=(const Matrix<T> &other);
friend std::ostream& operator<<(std::ostream &os , Matrix<T> m);
};
功能:
template <class T>
std::ostream& operator<<(std::ostream &os , Matrix<T> m){
for (size_t i = 0; i < m.rows; i++)
{
for (size_t j = 0; j < m.cols; j++)
{
os << m.mat[i][j] << " ";
}
os << std::endl;
}
return os;
}
主营:
int main(){
std::vector<int> v(9);
v[0] = 1;
v[1] = 2;
v[2] = 3;
v[3] = 4;
v[4] = 5;
v[5] = 6;
v[6] = 7;
v[7] = 8;
v[8] = 9;
Matrix<int> m = Matrix<int>(2, 3, v);
std::cout << m;
}
我得到这个错误:
错误1个错误LNK2019:解析外部符号 “类 的std :: basic_ostream> & __cdecl 操作者< <(类的std :: basic_ostream
&,类矩阵)”(?? 6 @ YAAAV?$ basic_ostream @ DU?$ char_traits @ D @ std @@@ std @@ AAV01 @ V?$ Matrix @ H @@@ Z) reference in function _main C:\ Users \ igor \ documents \ visual studio 2013 \ Projects \ matrix \ matrix \ Source.obj matrix
我试图写它没有朋友,但得到了不同的错误。
我在做什么错?
在'main.cpp'中是否包含'#include'd的.h文件中的定义?发布[MCVE](http://stackoverflow.com/help/mcve)将非常有用。 –
我不知道这是否与你的问题有任何关系(我不认为是这样),但你仍然应该有一个函数采取一个'常规矩阵&'而不是'Matrix ' –
Amxx
。包括h文件,我尝试了其他功能,它们工作正常。 –