Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/132.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 我真的不明白为什么我在创建模板类共享指针时会出错_C++_Pointers - Fatal编程技术网

C++ 我真的不明白为什么我在创建模板类共享指针时会出错

C++ 我真的不明白为什么我在创建模板类共享指针时会出错,c++,pointers,C++,Pointers,我不理解我收到的错误信息,我不知道如何修复它 template<typename T> class shared_pointer { private: static int howManyObjects; T* pointer; public: shared_pointer( T* p=nullptr) { pointer=p; } shared_p

我不理解我收到的错误信息,我不知道如何修复它

template<typename T>
class shared_pointer
{
    private:
        static int howManyObjects;
        T* pointer;
    public:
        shared_pointer( T* p=nullptr)
        {
            pointer=p;
        }
        shared_pointer( shared_pointer& a)
        {
            a.pointer=this->pointer;
            howManyObjects++;
        }
        ~shared_pointer()
        {
            if(howManyObjects==1) delete pointer;
        }

        T& operator *()
        {
            return *pointer;
        }
        T* operator ->()
        {
            return pointer;
        }
};
template<typename T>
int shared_pointer<T>::howManyObjects=0;

int main()
{
    int b=5;
    int* wsk=&b;
    shared_pointer<int> a= shared_pointer<int>(wsk);
    return 0;
}
模板
类共享指针
{
私人:
静态int有多少个对象;
T*指针;
公众:
共享_指针(T*p=nullptr)
{
指针=p;
}
共享指针(共享指针&a)
{
a、 指针=此->指针;
有多少个对象++;
}
~shared_pointer()
{
如果(howManyObjects==1)删除指针;
}
T&运算符*()
{
返回*指针;
}
T*运算符->()
{
返回指针;
}
};
模板
int shared_指针::howManyObjects=0;
int main()
{
int b=5;
int*wsk=&b;
共享指针a=共享指针(wsk);
返回0;
}
错误消息:

main.cpp:在函数“int main()”中:
main.cpp:10:25:错误:无法将“shared\u pointer&”类型的非常量左值引用绑定到“shared\u pointer”类型的右值
共享指针a=共享指针(wsk);
在main.cpp中包含的文件中:2:0:
smartpp.cpp:14:2:注意:初始化'shared_pointer::shared_pointer(shared_pointer&)[with T=int]的参数1'
共享指针(共享指针&a)

定义移动分配运算符

shared_pointer& operator=(shared_pointer&& other)
{
    pointer = other.pointer;
    return *this;
}
你错过了很多东西。我列举了一些

~shared_pointer()
不会减少引用计数。 因为这个
int*wsk=&b:您的
dtor
将删除堆栈中的指针,您不应该这样做。
howManyObjects
不应是静态的。如果您关心线程安全,也应该自动更改它。

您的问题在于复制构造函数:

shared_pointer( shared_pointer& a)
{
    a.pointer = this->pointer;
    howManyObjects++;
}
因此,根据参数a类型之前的空格,您可能知道它必须是一个常量,由复制构造函数规则决定。但是,当您尝试将
const
放在那里时,出现以下错误:

shared_pointer(const shared_pointer& a)
{
    a.pointer = this->pointer; // Compilation error: assignment of member ‘shared_pointer<int>::pointer’ in read-only object
    howManyObjects++;
}

复制构造函数应该获得const引用。为什么您要自己重新实现控制盘(aka
std::shared_ptr
)?只需使用标准库已经提供的内容,而不是自己(错误地)重新实现它。这并不能解决问题,但编写的复制构造函数是反向的:它将正在构造的对象中的(未初始化的)
指针
复制到正在复制的对象中(
a
)。另外,在
main
中,当共享指针试图删除堆栈上的对象时,创建指向该对象的共享指针会带来不好的结果。我还认为,
howManyObjects
静态字段可能不会达到预期的效果-
shared\u ptr
中的引用计数应该是
shared_ptr
存在于给定指针,而不是全局总数。@JesperJuhl通过重新创建类似的内容,我将了解更多信息。至少我是这么想的;
int*wsk=&b。它在堆栈上创建指向堆栈上对象的指针。它们在定义它们的块的末尾都超出了范围,因此没有生命周期问题。@PeteBecker你说得对,这里没有问题。只是一个一般性的建议。我编辑它感谢你对我做错了什么的好解释,这是迄今为止最好的答案。
shared_pointer(const shared_pointer& a)
{
    this->pointer = a.pointer; // Pay attention that this get the value of a, and not the opposite.
    howManyObjects++;
}