My template definition is,
Expand|Select|Wrap|Line Numbers
- template <class type>
- class Array
- {
- private:
- type* data;
- unsigned int iMax;
- unsigned int jMax;
- unsigned int kMax;
- public:
- Array()
- {
- data = 0;
- }
- Array(const Array<type>& _obj)
- {
- iMax = _obj.iMax;
- jMax = _obj.jMax;
- kMax = _obj.kMax;
- data = new type[iMax * jMax * kMax];
- for (unsigned int ijk = 0; ijk < iMax * jMax * kMax; ijk++)
- {
- data[ijk] = _obj.data[ijk];
- }
- }
- Array(const unsigned int _iMax, const unsigned int _jMax, const unsigned int _kMax)
- {
- iMax = _iMax;
- jMax = _jMax;
- kMax = _kMax;
- data = new type[iMax * jMax * kMax];
- }
- ~Array()
- {
- if (data != 0)
- {
- delete [] data;
- data = 0;
- }
- }
- Array<type>& operator=(const Array<type>& rhs)
- {
- if (&rhs != this)
- {
- iMax = rhs.iMax;
- jMax = rhs.jMax;
- kMax = rhs.kMax;
- if (data!= 0)
- {
- delete [] data;
- }
- data = new type[iMax * jMax * kMax];
- for (unsigned int ijk = 0; ijk < iMax * jMax * kMax; ijk++)
- {
- data[ijk] = rhs.data[ijk];
- }
- }
- return *this;
- }
- Array<type>& operator=(const type& rhs)
- {
- for (unsigned int ijk = 0; ijk < iMax * jMax * kMax; ijk++)
- {
- data[ijk] = rhs;
- }
- return *this;
- }
- friend Array<type> operator+(const type& lhs, const Array<type>& rhs);
- friend Array<type> operator-(const type& lhs, const Array<type>& rhs);
- };
- template <class type>
- Array<type> operator+(const type& lhs, const Array<type>& rhs)
- {
- Array<type> newMat(rhs);
- for (unsigned int ijk = 0; ijk < newMat.iMax * newMat.jMax * newMat.kMax; ijk++)
- {
- newMat.data[ijk] += lhs;
- }
- return newMat;
- }
- template <class type>
- Array<type> operator-(const type& lhs, const Array<type>& rhs)
- {
- Array<type> newMat(rhs);
- for (unsigned int ijk = 0; ijk < newMat.iMax * newMat.jMax * newMat.kMax; ijk++)
- {
- newMat.data[ijk] = lhs - newMat.data[ijk];
- }
- return newMat;
- }
Expand|Select|Wrap|Line Numbers
- #include "Array.h"
- int main(int argc, char* argv[])
- {
- Array<int> a0_t4(3, 3, 1);
- a0_t4 = 2;
- Array<int> a2_t4;
- a2_t4 = 1 + a0_t4;
- a2_t4 = 1 - a0_t4;
- return 0;
- }
Error 1 error LNK2019: unresolved external symbol "class Array<int> __cdecl operator-(int const &,class Array<int> const &)" (??G@YA?AV?$Array@H@@ABHABV0@@Z) referenced in function _main TestFlow.obj
Error 2 error LNK2019: unresolved external symbol "class Array<int> __cdecl operator+(int const &,class Array<int> const &)" (??H@YA?AV?$Array@H@@ABHABV0@@Z) referenced in function _main TestFlow.obj
Error 3 fatal error LNK1120: 2 unresolved externals C:\Documents and Settings\sstanley\Desktop\cppPlaying\TestFlow\Debu g\TestFlow.exe
Can anyone tell me what I am doing wrong in these operators?