I have customized a template class and overloaded the operator, and the predefined implementation function is capable of implementing an array of common operations, similar to vectors.
#ifndef array_h#define array_h#include <iostream>using namespace Std;template<class t>class Array{friend ostream& operator<< (ostream&, const Array &); friend istream& operator>> (istream& Array &);p ublic:array (int arraySize = ten) {size = (ArraySize > 0? arraysize:10);p tr = new T[size];for (int i = 0; i < size; i++) Ptr[i] = 0;} Array (const array &arraytocopy) {ptr = new int[size];for (int i = 0; i < size; i++) {ptr[i] = Arraytocopy[i];}} ~array () {delete[] ptr;} int GetSize () const{return size;} Const array& operator= (const Array &right) {if (&right! = this) {if (size! = right.size) {delete[] ptr;size = Rig Ht.size;ptr = new T[size];} for (int i = 0; i < size; i++) ptr[i] = Right.ptr[i];} return *this;} BOOL operator== (const Array &right) const{if (size! = right.size) return false;for (int i = 0; i < size; i++) if (ptr[ I] = Right.ptr[i]) return False;return true; BOOL Operator!= (const Array &right) Const{return! ( *this = = right);} t& operator[] (int subscript) {if (Subscript < 0 | | subscript >= size) {cerr << "\nerror:subscript" << ; subscript<< "Out of Range" << endl;exit (1);} return ptr[subscript];} T operator[] (int subscript) const{if (Subscript < 0 | | subscript >= size) {cerr << "\nerror:subscript" << subscript<< "Out of Range" << endl;exit (1);} return ptr[subscript];} Private:int size; T *ptr;}; #endiftemplate <class t>istream& operator>> (IStream &input, array<t> &a) {for (int i = 0; I & Lt A.size; i++) input >> A.ptr[i];return input;} Template<class t>ostream& operator<< (ostream &output, const array<t> &a) {int i;for (i = 0; i < a.size; i++) {Output << setw (+) << a.ptr[i];if ((i + 1)% 4 = = 0) ouput << Endl;} if (i% 4! = 0) output << endl;return output;}
#include <iostream> #include "Array.h" using namespace Std;int Main () {array<int> integers1 (7); cout << "Size of Array integers1 is" << integers1.getsize () << "\narray after initialization:\n" << integers1;}
The error occurred because
1>main.obj:error LNK2019: unresolved external symbol "Class Std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<< (class std::basic_ostream<char,struct std::char_traits<char> > &,class array<int> Const &) "([email protected][email protected][email protected]@[email Protected]@@[email Protected]@[email protected][email protected]@@@z), the symbol is referenced in the function _main
The reason I'm guessing is that CIN is a Ostream object instance, but I'm not doing << overloading my custom type, please God help me solve this problem
After the friend is not set
Header file
#ifndef array_h#define array_h#include <iostream>using namespace Std;template<class t>class Array{// Friend ostream& operator<< (ostream&, const Array &);//friend istream& operator>> (IStream &, Array &);p ublic:array (int arraySize = ten) {size = (ArraySize > 0? arraysize:10);p tr = new T[size];for (int i = 0; i < size; i++) Ptr[i] = 0;} Array (const array &arraytocopy) {ptr = new int[size];for (int i = 0; i < size; i++) {ptr[i] = Arraytocopy[i];}} ~array () {delete[] ptr;} int GetSize () const{return size;} Const array& operator= (const Array &right) {if (&right! = this) {if (size! = right.size) {delete[] ptr;size = Rig Ht.size;ptr = new T[size];} for (int i = 0; i < size; i++) ptr[i] = Right.ptr[i];} return *this;} BOOL operator== (const Array &right) const{if (size! = right.size) return false;for (int i = 0; i < size; i++) if (ptr[ I] = Right.ptr[i]) return False;return true; BOOL Operator!= (const Array &right) Const{return! ( *this = = right);} t& operator[] (int subscript) {if (Subscript < 0 | | subscript >= size) {cerr << "\nerror:subscript" << ; subscript<< "Out of Range" << endl;exit (1);} return ptr[subscript];} T operator[] (int subscript) const{if (Subscript < 0 | | subscript >= size) {cerr << "\nerror:subscript" << subscript<< "Out of Range" << endl;exit (1);} return ptr[subscript];} Private:int size; T *ptr;}; #endiftemplate <class t>istream& operator>> (IStream &input, array<t> &a) {for (int i = 0; I & Lt A.size; i++) input >> A.ptr[i];return input;} Template<class t>ostream& operator<< (ostream &output, const array<t> &a) {int i;for (i = 0; i < a.size; i++) {Output << setw (+) << a.ptr[i];if ((i + 1)% 4 = = 0) ouput << Endl;} if (i% 4! = 0) output << endl;return output;}
Main function
#include <iostream> #include "Array.h" using namespace Std;int Main () {array<int> integers1 (7); Array<int> integers2; Array<char> char_string (+); cout << "Size of Array integers1 is" << integers1.getsize () << "\ NArray after initialization:\n "<< endl;//<< integers1;cout <<" Size of the Array char_string is "<< C Har_string.getsize () << "\narray after initialization:\n" << endl;//<< integers1;if (integers1! = INTEGERS2) cout << "integers1 and integers2 are not equal" << endl;cout << "\ninteger2[5" is "<< I NTEGERS2[5] << Endl;}
Run
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Template classes and operator overloading