首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

,set_intersection处报错error C2678!

2013-01-07 
求助,set_intersection处报错error C2678!!刚学stl,写了一个关于set的小程序,结果就是编译不过,报错: 二进

求助,set_intersection处报错error C2678!!
刚学stl,写了一个关于set的小程序,结果就是编译不过,报错:
 二进制“=”: 没有找到接受“const st”类型的左操作数的运算符(或没有可接受的转换)
 \teststl\teststl.cpp(16): 可能是“st &st::operator =(const st &)”
按理说,出现这个错误需要重构一个 = 运算符了,并且需要是const型的,但一旦是const的,又不能对结构体里的变量赋值了啊!怎么破???

代码如下:


#include<iostream>
#include<set>
#include<string>
#include<algorithm>
using namespace std ;

typedef struct st{
string str ;
int i ;
}NODE, *PNODE;

struct LTNode
{
bool operator()( const NODE &nod1, const NODE &nod2 ) const 
{
return nod1.str<nod2.str ;
}
};

bool set_eq( NODE nod1, NODE nod2 )
{
return nod1.str < nod2.str ;
}

void print( const NODE &nod )
{
cout<<nod.str<<" "<<nod.i<<endl ;
}

int main()
{
set<NODE, LTNode> sa, sb, sc ;
NODE nod ;
nod.str = "A5" ;nod.i = 5 ;
sa.insert( nod ) ;
nod.str = "A1" ;nod.i = 1 ;
sa.insert( nod ) ;
nod.str = "A3" ;nod.i = 3 ;
sa.insert( nod ) ;
for_each( sa.begin(), sa.end(), print ) ;

nod.str = "A4" ;nod.i = 4 ;
sb.insert( nod ) ;
nod.str = "A2" ;nod.i = 2 ;
sb.insert( nod ) ;
for_each( sb.begin(), sb.end(), print ) ;

set_intersection( sa.begin(), sa.end(), sb.begin(), sb.end(), sc.begin(), set_eq ) ;
for_each( sc.begin(), sc.end(), print ) ;
system( "pause" ) ;
return 0 ;
}


[解决办法]
我在VC6.0下编译是没有问题的,这应该是STL实现的差别问题。LZ可以查一下你的STL下那个函数的定义,然后按照定义进行一下类型强制转换就应该可以解决问题
[解决办法]
set_intersection( sa.begin(), sa.end(), sb.begin(), sb.end(), sc.begin(), set_eq ) ;
问题出在这句上。

sc.begin()这个iterator不能用来写入set。vc6能过那是因为vc6不标准。
解决办法,要么把sc声明为vector<NODE>,要么#include<iterator>然后用std::inserter(sc, sc.begin())代替原来的sc.begin()

热点排行