| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1252 人关注过本帖
标题:求教这段程序哪错误
只看楼主 加入收藏
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
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册: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
快速回复:求教这段程序哪错误
数据加载中...
 
   



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

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