注册 登录
编程论坛 C++教室

求教这段程序哪错误

qq826647235 发布于 2017-02-27 23:27, 1438 次点击
程序代码:
#include<iostream>
#include<memory>
#include<string>
#include<vector>

using namespace std;

class Strblob
{
public:
    typedef vector<string>::size_type size_type;
    Strblob();
    Strblob(vector<string> a);
    void printfff()
    {
        for (auto a : *data)
        {
            cout << a << endl;
        }
    }
private:
    shared_ptr <vector<string>> data;
};
Strblob::Strblob() :data(make_shared<vector<string>>){}
Strblob::Strblob(vector<string>a): data(make_shared<vector<string>>(a)){}

int main()
{
    Strblob a = { { "a", "b", "c" } };
    Strblob b = a;
    b.printfff();
    getchar();
}


编译时出错了。说delete不能删除不是指针的对象。我搞不清楚哪段代码用了delete。
2 回复
#2
rjsp2017-02-28 08:29
程序代码:
#include <iostream>
#include <memory>
#include <string>
#include <vector>

class Strblob
{
public:
    typedef std::vector<std::string>::size_type size_type;
    Strblob();
    Strblob( const std::vector<std::string>& a );

private:
    std::shared_ptr<std::vector<std::string>> data_;

    friend std::ostream& operator<<( std::ostream& os, const Strblob& sb );
};

using namespace std;

Strblob::Strblob() : data_( std::make_shared<std::vector<std::string>>() )
{
}
Strblob::Strblob( const std::vector<std::string>& a ) : data_( std::make_shared<std::vector<std::string>>(a) )
{
}
std::ostream& operator<<( std::ostream& os, const Strblob& sb )
{
    for( auto a : *sb.data_ )
        os << a << '\n';
    return os;
}

int main( void )
{
    Strblob a = { { "a", "b", "c" } };
    Strblob b = a;
    cout << b << endl;
}
#3
qq8266472352017-02-28 16:39
回复 2楼 rjsp
为什么要单独加个友元
1