スマートポインタのインスタンスをコンテナに保存するのに問題があります。ポインタのコードは次のとおりです。
#include "std_lib_facilities.h"
template <class T>
class counted_ptr{
private:
T* pointer;
int* count;
public:
counted_ptr(T* p = 0, int* c = new int(1)) : pointer(p), count(c) {} // default constructor
explicit counted_ptr(const counted_ptr& p) : pointer(p.pointer), count(p.count) { ++*count; } // copy constructor
~counted_ptr()
{
--*count;
if(!*count) {
delete pointer;
delete count;
}
}
counted_ptr& operator=(const counted_ptr& p) // copy assignment
{
pointer = p.pointer;
count = p.count;
++*count;
return *this;
}
T* operator->() const{ return pointer; }
T& operator*() const { return *pointer; }
int& operator[](int index) { return pointer[index]; }
int Get_count() const { return *count; } // public accessor for count
};
int main()
{
counted_ptr<double>one;
counted_ptr<double>two(one);
one = new double(5);
vector<counted_ptr<double> >test;
}
int main()では、vector<counted_ptr<double> >
行はコンパイルされます。最初に試したときvector<counted_ptr<double> >
はコンパイルされませんでした(おそらくパラメーターが不足していたためです)。ただし、次のようなpush_backを使用しようとすると
test.push_back(one);
vector.tccを開くコンパイラエラーが発生し、特定のエラーが発生します。
no matching function for call to `counted_ptr<double>::counted_ptr(const counted_ptr<double>&)'|
push_backはcounted_ptrを見つけることができないと思いますが、本当にわかりません。どんな助けでもありがたいです、ありがとう。
編集:ただし、これは機能します。test [0] =1; push_backのセマンティクスがそれを制限していると思います。