I found this code on the web, after fixing some typo I got it running then I added operators to the BlobPtr template and now it doesn't compile anymore. It's already the smallest piece and it returns the same error messages of the main code I wrote.
Could anybody take a look and give me any hints?Thanks for your patienceCode:#include <iostream> #include <memory> #include <vector> using namespace std; template<typename> class BlobPtr; template<typename T> class Blob { friend class BlobPtr<T>; public: typedef T value_type; typedef typename vector<T>::size_type size_type; // constructors Blob(); Blob(initializer_list<T> il); // number of elements in the Blob size_type size() const { return data->size(); } bool empty() const{ return data->empty(); } void push_back(const T& t) { data->push_back(t); } void push_back(T&& t) { data->push_back(move(t)); } void pop_back(); // element access T& back(); T& operator[](size_type i); const T& back() const; const T& operator [](size_type i) const; private: shared_ptr<vector<T>> data; // throw msg if data[i] isn't valid void check(size_type i, const string &msg) const; }; // constructors template<typename T> Blob<T>::Blob() : data(make_shared<vector<T>>()) { } template<typename T> Blob<T>::Blob(initializer_list<T> il): data(make_shared<vector<T>>(il)){ } template<typename T> void Blob<T>::check(size_type i, const string &msg) const { if(i >= data->size()) throw out_of_range(msg); } template<typename T> T& Blob<T>::back() { check(0, "back on empty Blob"); return data->back(); } template<typename T> const T& Blob<T>::back() const { check(0, "back on empty Blob"); return data->back(); } template<typename T> T& Blob<T>::operator [](size_type i) { // if i is too big, check function will throw, preventing access to a nonexistent element check(i, "subscript out of range"); return (*data)[i]; } template<typename T> const T& Blob<T>::operator [](size_type i) const { // if i is too big, check function will throw, preventing access to a nonexistent element check(i, "subscript out of range"); return (*data)[i]; } template<typename T> void Blob<T>::pop_back() { check(0, "pop_back on empty Blob"); data->pop_back(); } template <typename> class BlobPtr; template <typename T> bool operator ==(const BlobPtr<T>& lhs, const BlobPtr<T>& rhs); template <typename T> bool operator < (const BlobPtr<T>& lhs, const BlobPtr<T>& rhs); template <typename T> BlobPtr<T> operator+(const BlobPtr<T>&, const ptrdiff_t); template <typename T> BlobPtr<T> operator-(const BlobPtr<T>&, const ptrdiff_t); template<typename T> class BlobPtr { friend bool operator ==<T>(const BlobPtr<T>& lhs, const BlobPtr<T>& rhs); friend bool operator < <T>(const BlobPtr<T>& lhs, const BlobPtr<T>& rhs); friend BlobPtr<T> operator+<T>(const BlobPtr<T>&, const ptrdiff_t); friend BlobPtr<T> operator-<T>(const BlobPtr<T>&, const ptrdiff_t); public: BlobPtr() : curr(0) { } BlobPtr(Blob<T>& a, size_t sz = 0): wptr(a.data), curr(sz) { } T& operator*() const { auto p = check(curr, "dereference past end"); return (*p)[curr]; } BlobPtr& operator+=(const ptrdiff_t); BlobPtr& operator-=(const ptrdiff_t); // prefix BlobPtr& operator++(); BlobPtr& operator--(); // postfix BlobPtr<T> operator ++(int); BlobPtr<T> operator --(int); private: // returns a shared_ptr to the vector if the check succeeds shared_ptr<vector<T>> check(size_t, const string&) const; weak_ptr<vector<T>> wptr; size_t curr; }; template<typename T> shared_ptr<vector<T>> BlobPtr<T>::check(size_t i, const string& msg) const { auto ret = wptr.lock(); // is the vector still around? if (!ret) throw runtime_error("unbound BlobPtr"); if (i >= ret->size()) throw out_of_range(msg); return ret; // otherwise, return a shared_ptr to the vector } // prefix ++ template<typename T> BlobPtr<T>& BlobPtr<T>::operator ++() { // if curr already points past the end of the container, can't increment it check(curr, "increment past end of StrBlob"); ++curr; return *this; } // prefix -- template<typename T> BlobPtr<T>& BlobPtr<T>::operator --() { -- curr; check(curr, "decrement past begin of BlobPtr"); return *this; } // postfix ++ template<typename T> BlobPtr<T> BlobPtr<T>::operator ++(int) { BlobPtr ret = *this; ++*this; return ret; } // postfix -- template<typename T> BlobPtr<T> BlobPtr<T>::operator --(int) { BlobPtr ret = *this; --*this; return ret; } template<typename T> bool operator==(const BlobPtr<T> &lhs, const BlobPtr<T> &rhs) { return lhs.curr == rhs.curr; } template<typename T> bool operator< (const BlobPtr<T> &lhs, const BlobPtr<T> &rhs) { return lhs.curr < rhs.curr; } template <typename T> BlobPtr<T>& BlobPtr<T>::operator+=(const ptrdiff_t n) { curr += n; check(curr, "addition past end of BlobPtr"); return *this; } template <typename T> BlobPtr<T>& BlobPtr<T>::operator-=(const ptrdiff_t n) { curr -= n; check(curr, "subtraction past begin of BlobPtr"); return *this; } template <typename T> BlobPtr<T> BlobPtr<T>::operator+(const BlobPtr<T>& lhs, const ptrdiff_t n) { return (BlobPtr<T>(lhs)).operator+=(n); } template <typename T> BlobPtr<T> BlobPtr<T>::operator-(const BlobPtr<T>& lhs, const ptrdiff_t n) { return (BlobPtr<T>(lhs)).operator-=(n); } int main() { Blob<string> b; BlobPtr<string> bp(b); return EXIT_SUCCESS; }