I'm new to C++. Just out of curiosity, I'm trying to see what happens when I try to overload the insertion ">>" operator for a class of dynamic array. I think what I'm trying to do is impossible. But could anyone explain what this error means? (error below the code)
#include<iostream> using namespace std; template<class Type> class Array{ private: int length; Type* ptrarr; public: int size() const; Type* get_array() const; friend istream& operator >> (istream& s, Array<Type>& arr); }; template<class Type> int Array<Type>::size() const{ return length; } template<class Type> Type* Array<Type>::get_array() const{ return ptrarr; } template<class Type> istream& operator >> (istream& s, Array<Type>& arr){ cout << "Enter length of array"; cin >> arr.length; arr.ptrarr = new Type[arr.length]; for(int i = 0; i < arr.length; i++){ cout << "Array[" << i << "] = "; cin >> *(arr.ptrarr + i); } return s; } int main(){ Array<int> intarray; cin >> intarray; int* ptr = intarray.get_array(); for(int i =0; i < intarray.size(); i++) cout << *(ptr+i); return 0; }
I get the error
in function `main': dyn_array.cpp:(.text+0x1e): undefined reference to `operator>>(std::istream&, Array<int>&)' clang: error: linker command failed with exit code 1 (use -v to see invocation)
https://stackoverflow.com/questions/66978577/why-is-it-not-possible-to-overload-insertion-operators-in-c-for-a-dynamic-arra April 07, 2021 at 09:54AM
没有评论:
发表评论