错误 1 error LNK2001: 无法解析的外部符号 "public: __thiscall illegalParameterValue::~illega

赵小咖 2017-10-21 09:16:14

//matrix.h
#pragma once
#include<iostream>
using namespace std;


template<class T>
class matrix
{
friend ostream& operator<<(ostream&, const matrix<T>&);
public:
matrix(int theRows = 0, int theColumes = 0);
matrix(const matrix<T>&);
~matrix(){ delete[] elements; }
int rows(){ return theRows; }
int columes(){ return theColumes; }
T& operator()(int i, int j) const;
matrix<T> operator+(const matrix<T>&) const;
matrix<T> operator+() const;
matrix<T>& operator=(const matrix<T>&);
matrix<T> operator-(const matrix<T>&) const;
matrix<T> operator-() const;
matrix<T> operator*(const matrix<T>&) const;
matrix<T> operator+=(const T&);
private:
int theRows, theColumes;
T *elements;
};

template<class T>
ostream& operator<<(ostream& out, const matrix<T>& m)
{// Put matrix m into the stream out.
// One row per line.
int k = 0; // index into element array
for (int i = 0; i < m.theRows; i++)
{// do row i
for (int j = 0; j < m.theColumes; j++)
out << m.elements[k++] << " ";

// row i finished
out << endl;
}
return out;
}



//matrix.cpp
#include "matrix.h"
#include "illegalParameterValue.h"


template<class T>
matrix<T>::matrix(int theRows,int theColumes)
{
if (theRows < 0 || theColumes < 0)
throw illegalParameterValue("Row and Columes must be >=0");
if ((theRows != 0 || theColumes != 0) && (theRows == 0 || theColumes == 0))
throw illegalParameterValue("Either both or neither Rows and Columes should be zero");
this->theRows = theRows;
this->theColumes = theColumes;
elements = new T[theRows*theColumes];
}


template<class T>
matrix<T>::matrix(const matrix<T>& m)
{
theRows = m.theRows;
theColumes = m.theColumes;
elements = new T[theColumes*theRows];
copy(m.elements, m.elements + theColumes*theRows, elements);
}


template<class T>
matrix<T>& matrix<T>::operator=(const matrix<T>& m)
{
if (this != &m)
{
delete[] elements;
theRows = m.theRows;
theColumes = m.theColumes;
elements = new T[theColumes*theRows];
copy(m.elements, m.elements + theRows*theColumes, elements);
}
return *this;
}


template<class T>
T& matrix<T>::operator()(int i, int j) const
{
if (i<1 || i>theRows || j > 1 || j > theColumes)
throw matrixIndexOutOfBrounds();
return elements[(i - 1)*theColumes + j - 1];
}



template<class T>
matrix<T> matrix<T>::operator+(const matrix<T>& m) const
{
if (theRows != m.theRows || theColumes != m.theColumes)
throw matrixSizeMismatch();
matrix<T> w(theRows, theColumes);
for (int i = 0; i < theRows*theColumes; i++)
w.elements[i] = m.elements[i] + elements[i];
return w;
}


template<class T>
matrix<T> matrix<T>::operator*(const matrix<T>& m) const
{
if (theColumes != m.theRows)
throw matrixSizeMismatch();
matrix<T> w(theRows, m.theColumes);
int ct = 0, cm = 0, cw = 0;
for (int i = 1; i <= theRows; i++)
{
for (int j = 0; j <= theColumes; j++)
{
T sum = elements[ct] * m.elements[cm];
for (int k = 0; k <= theColumes; k++)
{
ct++;
cm += m.theColumes;
sum += elements[ct] * m.elements[cm];
}
w.elements[cw++] = sum;
ct -= theColumes - 1;
cm = j;
}
ct += theColumes;
cm = 0;
}
return w;
}


//main.cpp
#include<iostream>
#include<string>
#include"matrix.h"

using namespace std;

int main()
{
matrix<int> mat(4,5);

return 0;
}





错误如下:
错误 1 error LNK2001: 无法解析的外部符号 "public: __thiscall illegalParameterValue::~illegalParameterValue(void)" (??1illegalParameterValue@@QAE@XZ) C:\Users\zhao\Documents\Visual Studio 2013\Projects\matrix\matrix\main.obj matrix
错误 2 error LNK1120: 1 个无法解析的外部命令
...全文
319 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
真相重于对错 2017-10-22
  • 打赏
  • 举报
回复
模板的声明和定义,不要分为两个文件!
paschen 版主 2017-10-22
  • 打赏
  • 举报
回复
paschen 版主 2017-10-22
  • 打赏
  • 举报
回复
编译器不支持对模板的分离式编译,将类模板的声明和实现放到一个文件中

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧