compiling error while using unique or unique_copy with multiset.

I defined a class with the name of wordMultiSet, inside there is
a private variable: multiset<string, CompareWords> wordset;
then in one of the member function I tried to create a temporary multiset to store the set of string after deleting the duplicates in wordset, like the following,

1
2
  multiset<string,CompareWords> temp = wordset;
	auto it = unique(temp.begin(), temp.end());

but I got the compiling error:

`1>------ Build started: Project: ConsoleApplication6, Configuration: Debug Win32 ------
1> WordMultiSet.cpp
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\algorithm(1825): error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const std::basic_string<_Elem,_Traits,_Alloc>' (or there is no acceptable conversion)
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Alloc=std::allocator<char>
1> ]
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring(912): could be 'std::basic_string<_Elem,_Traits,_Alloc> &std::basic_string<_Elem,_Traits,_Alloc>::operator =(std::basic_string<_Elem,_Traits,_Alloc> &&) throw()'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Alloc=std::allocator<char>
1> ]
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring(969): or 'std::basic_string<_Elem,_Traits,_Alloc> &std::basic_string<_Elem,_Traits,_Alloc>::operator =(const std::basic_string<_Elem,_Traits,_Alloc> &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Alloc=std::allocator<char>
1> ]
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring(987): or 'std::basic_string<_Elem,_Traits,_Alloc> &std::basic_string<_Elem,_Traits,_Alloc>::operator =(const _Elem *)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Alloc=std::allocator<char>
1> ]
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring(992): or 'std::basic_string<_Elem,_Traits,_Alloc> &std::basic_string<_Elem,_Traits,_Alloc>::operator =(_Elem)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Alloc=std::allocator<char>
1> ]
1> while trying to match the argument list '(const std::basic_string<_Elem,_Traits,_Alloc>, const std::basic_string<_Elem,_Traits,_Alloc>)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Alloc=std::allocator<char>
1> ]
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\algorithm(1836) : see reference to function template instantiation '_FwdIt std::_Unique<std::_Tree_unchecked_const_iterator<_Mytree>>(_FwdIt,_FwdIt)' being compiled
1> with
1> [
1> _FwdIt=std::_Tree_unchecked_const_iterator<std::_Tree_val<std::_Tree_simple_types<std::basic_string<char,std::char_traits<char>,std::allocator<char>>>>>,
1> _Mytree=std::_Tree_val<std::_Tree_simple_types<std::basic_string<char,std::char_traits<char>,std::allocator<char>>>>
1> ]
1> f:\cpp\consoleapplication6\wordmultiset.cpp(53) : see reference to function template instantiation '_FwdIt std::unique<std::_Tree_const_iterator<_Mytree>>(_FwdIt,_FwdIt)' being compiled
1> with
1> [
1> _FwdIt=std::_Tree_const_iterator<std::_Tree_val<std::_Tree_simple_types<std::basic_string<char,std::char_traits<char>,std::allocator<char>>>>>,
1> _Mytree=std::_Tree_val<std::_Tree_simple_types<std::basic_string<char,std::char_traits<char>,std::allocator<char>>>>
1> ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
`






can anybody tell me what I did wrong ?

thanks
can anybody tell me what I did wrong ?


Not without seeing more of your code. So please post the smallest possible complete program that illustrates your problem.

In a set or multiset, the elements serve as keys in a data structure, and being able to modify those elements stored in the data structure would violate the constraints of the data structure, so all iterators in a set or multiset are the equivalent of const_iterators. std::unique requires that dereferencing the iterators fed to it results in a type that is move assignable. This isn't satisfied by set or multiset iterators.

If you only want unique elements:
set<string,CompareWords> temp(wordset.begin(), wordset.end()) ;
use a set instead of a multiset.



The following is the declaration of class WordMultiSet:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class WordMultiSet
{

public:
	class CompareWords
	{
	public:
		bool operator()(const string s1, const string s2);
	};
	WordMultiSet(istream &);
	......
	void operator()(string wd); // operator overloading
	void printelement(const string ws) const; 
private:
	multiset<string, CompareWords> wordset;


};


the following is the definition of function void print(), it works if I change temp to vector<string> like what you see below,
but, everytime when I change temp to set or multiset, and make temp the copy of
wordset, then try to use unique or unique_copy temp ,there will be a compile error!

1
2
3
4
5
6
7
8
// this function works
void WordMultiSet::print()
{
    vector<std::string> temp(wordset.size()); // works with vector
    auto it=unique_copy(wordMultiSet.begin(),wordMultiSet.end(),temp.begin());
    temp.resize(distance(temp.begin(),it));
   for_each( temp.cbegin(), temp.cend(),  bind1st(mem_fun(&WordMultiSet::printelement), this));
}


this version doesn't work:
1
2
3
4
5
6
7

//  doesn't work with multiset
void WordMultiSet::print()
{
     multiset<string,CompareWords>  temp(wordset);
     auto it = unique(temp.begin(), temp.end())......
}
Last edited on
See my previous post in this thread.
Topic archived. No new replies allowed.