简体   繁体   English

使用智能指针时出现“没有运算符“[]”匹配这些操作数”如何解决

[英]How to solve "no operator "[]" matches these operands" when I use smart pointer

Here is my code:这是我的代码:

class Mystring
{
private:
    unsigned int size;
    shared_ptr<char>message=make_shared<char>();
public:
    Mystring(const char* input):size(strlen(input)),message(make_shared<char>(size+1))
    {cout<<"Created"<<endl;memcpy(this->message,input,size+1);}
    Mystring(const Mystring& other):size(other.size),message(make_shared<char>(size+1))
    {cout<<"Created"<<endl;memcpy(this->message,other.message,size+1);}
    ~Mystring(){cout<<"Deleted"<<endl;}
    friend ostream& operator<<(ostream& Cout,const Mystring& mystring)
    {Cout<<mystring.message<<endl;return Cout;}
    char& operator[](const unsigned int  index)
    {return this->message[index];}
};

I want to use smart pointers and operator [] properly.我想正确使用智能指针和operator [] How can I solve these problems:我该如何解决这些问题:

 no suitable conversion function from "std::shared_ptr<char>" to "void *" exists no operator "[]" matches these operands

So lots of problems with this code这段代码有很多问题

  1. You have a shared char array, so the type is shared_ptr<char[]> not shared_ptr<char> .您有一个共享的 char 数组,因此类型是shared_ptr<char[]>而不是shared_ptr<char> This also means you cannot use make_shared , since it doesn't work with arrays.这也意味着您不能使用make_shared ,因为它不适用于 arrays。

  2. If you want to access the pointer to the array use get() .如果要访问指向数组的指针,请使用get()

  3. Your copy constructor allocates a new array, which is a bit weird since why are you using shared_ptr , if you don't want to share the array?您的复制构造函数分配了一个新数组,这有点奇怪,因为如果您不想共享数组,为什么要使用shared_ptr On the other hand the default assignment operator will share the array, so you have a strange situation where sometimes when you copy Mystring you will share the array, and sometimes you won't.另一方面,默认的赋值运算符将共享数组,所以你会遇到一种奇怪的情况,有时当你复制Mystring时你会共享数组,有时你不会。

  4. Various other minor issues其他各种小问题

Here's a version that works这是一个有效的版本

class Mystring
{
private:
    size_t size;
    shared_ptr<char[]> message;
public:
    Mystring(const char* input) : size(strlen(input)), message(new char[size+1])
    {
        cout<<"Created"<<endl;
        memcpy(message.get(), input, size+1);
    }
    Mystring(const Mystring& other): size(other.size), message(other.message)
    {
        cout<<"Created"<<endl;
    }
    ~Mystring()
    {
        cout<<"Deleted"<<endl;
    }
    friend ostream& operator<<(ostream& Cout,const Mystring& mystring)
    {
        Cout<<mystring.message.get()<<endl;return Cout;
    }
    char& operator[](size_t index)
    {
        return message.get()[index];
    }
};

I changed the copy constructor so that it does share the underlying array.我更改了复制构造函数,以便它共享底层数组。 You can change it back if you wish.如果你愿意,你可以把它改回来。 However is you want a Mystring which does not share it's array with other Mystring objects then it would make more sense to use unique_ptr<char[]> instead of shared_ptr .但是,如果您想要一个不与其他Mystring对象共享其数组的Mystring ,那么使用unique_ptr<char[]>而不是shared_ptr会更有意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM