| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 7137 人关注过本帖
标题:程序出现错误,尝试引用已删除的函数,是怎么回事
只看楼主 加入收藏
y442974010f
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2016-7-28
结帖率:0
收藏
已结贴  问题点数:10 回复次数:4 
程序出现错误,尝试引用已删除的函数,是怎么回事
#include<iostream>
#include<iterator>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;

template<class T>
class My_ostream_iterator :public iterator<output_iterator_tag, T> {
private:
    string sep; //分隔符
    ostream & os;
public:
    My_ostream_iterator(ostream & o, string s) :sep(s), os(o) { }
    void operator ++() { }; // ++只需要有定义即可, 不需要做什么
    My_ostream_iterator & operator * () { return *this; }
    My_ostream_iterator & operator = (const T & val)
    {
        os << val << sep; return *this;
    }
};

int main() {
    int a[4] = { 1,2,3,4 };
    My_ostream_iterator<int> oit(cout, "*");
    copy(a, a + 4, oit); //输出 1*2*3*4*
    return 0;
}

程序出现了错误,错误是:
    C2280    “My_ostream_iterator<int> &My_ostream_iterator<int>::operator =(const My_ostream_iterator<int> &)”: 尝试引用已删除的函数    ConsoleApplication4    d:\program files (x86)\microsoft visual studio 14.0\vc\include\xutility    458

请教各个大神这是怎么回事??
搜索更多相关主题的帖子: private include public return 做什么 
2016-09-16 10:31
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:10 
错了,已删除


[此贴子已经被作者于2016-9-19 08:42编辑过]

2016-09-16 17:17
y442974010f
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2016-7-28
收藏
得分:0 
回复 2楼 rjsp
大神你好,我是新手,不是很明白你的意思,可以说得详细一些吗
2016-09-18 16:31
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:0 
void operator ++() { };
改为
My_ostream_iterator& operator++() { return *this; }
试试
2016-09-19 08:29
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:0 
引用类型是不可以重新引用到别的对象的
所以一旦你的类中有了引用,就没法隐式生成一个 operator= 了

程序代码:
#include <iostream>
using namespace std;

struct foo
{
    const int& value;

    foo( const int& v ) : value(v)
    {
    }

    foo& operator=( const foo& rhs )
    {
        // 你没法让 this->value 重新引用到 rhs.value
        return *this;
    }
};

int main( void )
{
    foo a( 1 );
    foo b( 2 );
    a = b;
    cout << a.value << endl;
    cout << b.value << endl;

    return 0;
}

2016-09-19 08:50
快速回复:程序出现错误,尝试引用已删除的函数,是怎么回事
数据加载中...
 
   



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

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