// cFunc.h
#pragma once
#include <istream>
#include <ostream>
//#include <iostream>
#include <cstdlib>
using namespace std;
/*using std::cout;
using std::endl;
using std::cin;
using std::ostream;*/
//template <class Type>
//ostream& operator<<(ostream& cout ,const Vecmatrix<Type>& result);
template <class Type>
class Vecmatrix
{
public:
Vecmatrix(int r,int c);
Vecmatrix( const Vecmatrix<Type>& result);
~Vecmatrix();
Type& operator()(int r,int c) const;
//Vecmatrix<Type> operator= ();
Vecmatrix<Type> operator= (const Vecmatrix<Type>& result) const;
//Vecmatrix<Type> operator+ () const;
Vecmatrix<Type> operator+ (const Vecmatrix<Type>& cresult) const;
//Vecmatrix<Type> operator- ();
Vecmatrix<Type> operator- (const Vecmatrix<Type>& cresult) const;
Vecmatrix<Type> operator* (Type count) const;
void ScreenList(const Vecmatrix<Type>& result);
friend ostream& operator<< <Type>(ostream& cout ,const Vecmatrix<Type>& result);
private:
mutable int rows;
mutable int cols;
mutable Type *elems;
};
template <class Type>
Vecmatrix<Type>::Vecmatrix(int r,int c)
{
if(r>0 && c>0)
{
rows=r;
cols=c;
elems = new Type[rows*cols];
}
//else
//error("The number of Matrix's rows and cloums cannot be negative!");
}
template <class Type>
Vecmatrix<Type>::~Vecmatrix()
{
delete []elems;
elems = NULL;
}
template <class Type>
Type& Vecmatrix<Type>::operator ()(int r, int c) const
{
return elems[(r-1)*cols+c-1];
}
template <class Type>
Vecmatrix<Type> Vecmatrix<Type>::operator+ (const Vecmatrix<Type>& cresult) const
{
if (rows == cresult.rows && cols ==cresult.cols)
{
Vecmatrix<Type> result(rows,cols);
for(int i=0; i<rows*cols; i++)
{
result.elems[i] = elems[i]+cresult.elems[i];
}
return result;
}
//else
//error("The rows and colums of matrix must equal!")
}
template <class Type>
Vecmatrix<Type> Vecmatrix<Type>::operator= (const Vecmatrix<Type>& result) const
{
int i;
rows = result.rows;
cols = result.cols;
for(i=0; i<rows*cols; i++);
{
elems[i] = result.elems[i];
}
return *this;
}
template <class Type>
Vecmatrix<Type> Vecmatrix<Type>::operator-(const Vecmatrix<Type>& cresult) const
{
if (rows == cresult.rows && cols ==cresult.cols)
{
Vecmatrix<Type> result(rows,cols);
for(int i=0; i<rows*cols; i++)
{
result.elems[i] = elems[i]-cresult.elems[i];
}
return result;
}
//else
//error("The rows and colums of matrix must equal!")
}
template <class Type>
ostream& operator<< (ostream& cout ,const Vecmatrix<Type>& result)
{
for(int i=0; i<result.rows*result.cols; i++)
{
cout<<result.elems[i]<<" ";
if((i+1)%result.cols == 0)
{
cout<<endl;
}
}
return cout;
}
//#endif
// TextProj.cpp : 定义控制台应用程序的入口点。
#include "stdafx.h"
#include <iostream>
#include "cFunc.h"
int main()
{
Vecmatrix<int> M(2,3);
M(1,1) = 1;
M(1,2) = 10;;
M(1,3) = 11;
M(2,1) = 5;
M(2,2) = 23;
M(2,3) = 15;
Vecmatrix<int> N(2,3);
N(1,1) = 2;
N(1,2) = 6;;
N(1,3) = 8;
N(2,1) = 9;
N(2,2) = 10;
N(2,3) = 30;
Vecmatrix<int> P(2,3);
P=N;
cout<< "第二行第三列的元素值为:"<<endl;
cout<<M(1,2)<<endl;
cout<<"M= "<<endl;
cout<< M <<endl;
cout<<"N= "<<endl;
cout<< N<<endl;
cout<<P<<endl;
getchar();
}
1>------ 已启动生成: 项目: TextProj, 配置: Debug Win32 ------
1>正在编译...
1>TextProj.cpp
1>正在链接...
1>TextProj.obj : error LNK2019: 无法解析的外部符号 "public: __thiscall Vecmatrix<int>::Vecmatrix<int>(class Vecmatrix<int> const &)" (??0?$Vecmatrix@H@@QAE@ABV0@@Z),该符号在函数 "public: class Vecmatrix<int> __thiscall Vecmatrix<int>::operator=(class Vecmatrix<int> const &)const " (??4?$Vecmatrix@H@@QBE?AV0@ABV0@@Z) 中被引用
1>F:\VC ++\TextProj\Debug\TextProj.exe : fatal error LNK1120: 1 个无法解析的外部命令
1>生成日志保存在“file://f:\VC ++\TextProj\TextProj\Debug\BuildLog.htm”
1>TextProj - 2 个错误,0 个警告
========== 生成: 0 已成功, 1 已失败, 0 最新, 0 已跳过 ==========
#pragma once
#include <istream>
#include <ostream>
//#include <iostream>
#include <cstdlib>
using namespace std;
/*using std::cout;
using std::endl;
using std::cin;
using std::ostream;*/
//template <class Type>
//ostream& operator<<(ostream& cout ,const Vecmatrix<Type>& result);
template <class Type>
class Vecmatrix
{
public:
Vecmatrix(int r,int c);
Vecmatrix( const Vecmatrix<Type>& result);
~Vecmatrix();
Type& operator()(int r,int c) const;
//Vecmatrix<Type> operator= ();
Vecmatrix<Type> operator= (const Vecmatrix<Type>& result) const;
//Vecmatrix<Type> operator+ () const;
Vecmatrix<Type> operator+ (const Vecmatrix<Type>& cresult) const;
//Vecmatrix<Type> operator- ();
Vecmatrix<Type> operator- (const Vecmatrix<Type>& cresult) const;
Vecmatrix<Type> operator* (Type count) const;
void ScreenList(const Vecmatrix<Type>& result);
friend ostream& operator<< <Type>(ostream& cout ,const Vecmatrix<Type>& result);
private:
mutable int rows;
mutable int cols;
mutable Type *elems;
};
template <class Type>
Vecmatrix<Type>::Vecmatrix(int r,int c)
{
if(r>0 && c>0)
{
rows=r;
cols=c;
elems = new Type[rows*cols];
}
//else
//error("The number of Matrix's rows and cloums cannot be negative!");
}
template <class Type>
Vecmatrix<Type>::~Vecmatrix()
{
delete []elems;
elems = NULL;
}
template <class Type>
Type& Vecmatrix<Type>::operator ()(int r, int c) const
{
return elems[(r-1)*cols+c-1];
}
template <class Type>
Vecmatrix<Type> Vecmatrix<Type>::operator+ (const Vecmatrix<Type>& cresult) const
{
if (rows == cresult.rows && cols ==cresult.cols)
{
Vecmatrix<Type> result(rows,cols);
for(int i=0; i<rows*cols; i++)
{
result.elems[i] = elems[i]+cresult.elems[i];
}
return result;
}
//else
//error("The rows and colums of matrix must equal!")
}
template <class Type>
Vecmatrix<Type> Vecmatrix<Type>::operator= (const Vecmatrix<Type>& result) const
{
int i;
rows = result.rows;
cols = result.cols;
for(i=0; i<rows*cols; i++);
{
elems[i] = result.elems[i];
}
return *this;
}
template <class Type>
Vecmatrix<Type> Vecmatrix<Type>::operator-(const Vecmatrix<Type>& cresult) const
{
if (rows == cresult.rows && cols ==cresult.cols)
{
Vecmatrix<Type> result(rows,cols);
for(int i=0; i<rows*cols; i++)
{
result.elems[i] = elems[i]-cresult.elems[i];
}
return result;
}
//else
//error("The rows and colums of matrix must equal!")
}
template <class Type>
ostream& operator<< (ostream& cout ,const Vecmatrix<Type>& result)
{
for(int i=0; i<result.rows*result.cols; i++)
{
cout<<result.elems[i]<<" ";
if((i+1)%result.cols == 0)
{
cout<<endl;
}
}
return cout;
}
//#endif
// TextProj.cpp : 定义控制台应用程序的入口点。
#include "stdafx.h"
#include <iostream>
#include "cFunc.h"
int main()
{
Vecmatrix<int> M(2,3);
M(1,1) = 1;
M(1,2) = 10;;
M(1,3) = 11;
M(2,1) = 5;
M(2,2) = 23;
M(2,3) = 15;
Vecmatrix<int> N(2,3);
N(1,1) = 2;
N(1,2) = 6;;
N(1,3) = 8;
N(2,1) = 9;
N(2,2) = 10;
N(2,3) = 30;
Vecmatrix<int> P(2,3);
P=N;
cout<< "第二行第三列的元素值为:"<<endl;
cout<<M(1,2)<<endl;
cout<<"M= "<<endl;
cout<< M <<endl;
cout<<"N= "<<endl;
cout<< N<<endl;
cout<<P<<endl;
getchar();
}
1>------ 已启动生成: 项目: TextProj, 配置: Debug Win32 ------
1>正在编译...
1>TextProj.cpp
1>正在链接...
1>TextProj.obj : error LNK2019: 无法解析的外部符号 "public: __thiscall Vecmatrix<int>::Vecmatrix<int>(class Vecmatrix<int> const &)" (??0?$Vecmatrix@H@@QAE@ABV0@@Z),该符号在函数 "public: class Vecmatrix<int> __thiscall Vecmatrix<int>::operator=(class Vecmatrix<int> const &)const " (??4?$Vecmatrix@H@@QBE?AV0@ABV0@@Z) 中被引用
1>F:\VC ++\TextProj\Debug\TextProj.exe : fatal error LNK1120: 1 个无法解析的外部命令
1>生成日志保存在“file://f:\VC ++\TextProj\TextProj\Debug\BuildLog.htm”
1>TextProj - 2 个错误,0 个警告
========== 生成: 0 已成功, 1 已失败, 0 最新, 0 已跳过 ==========