标题:求教这段程序哪错误
只看楼主
qq826647235
Rank: 2
等 级:论坛游民
帖 子:37
专家分:10
注 册:2016-5-4
结帖率:63.64%
已结贴  问题点数:20 回复次数:2 
求教这段程序哪错误
程序代码:
#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。
2017-02-27 23:27
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:507
帖 子:8890
专家分:53117
注 册:2011-1-18
得分:20 
程序代码:
#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;
}
2017-02-28 08:29
qq826647235
Rank: 2
等 级:论坛游民
帖 子:37
专家分:10
注 册:2016-5-4
得分:0 
回复 2楼 rjsp
为什么要单独加个友元
2017-02-28 16:39



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-474602-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.158090 second(s), 7 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved