Adding vector to NS2; Compiler error - c++

I tried to add # include <vector> to a NS2 simulator file but i'm facing problems.
i'm getting this error (I've used another computer with g++ 4.6 rather than old g++ 4.4).
why only including <vector> causes error?
`g++ -c -Wall -Wno-write-strings -DTCP_DELAY_BIND_ALL -DNO_TK -DTCLCL_CLASSINSTVAR -DNDEBUG -DLINUX_TCP_HEADER -DUSE_SHM -DHAVE_LIBTCLCL -DHAVE_TCLCL_H -DHAVE_LIBOTCL1_14 -DHAVE_OTCL_H -DHAVE_LIBTK8_5 -DHAVE_TK_H -DHAVE_LIBTCL8_5 -DHAVE_TCLINT_H -DHAVE_TCL_H -DHAVE_CONFIG_H -DNS_DIFFUSION -DSMAC_NO_SYNC -DCPP_NAMESPACE=std -DUSE_SINGLE_ADDRESS_SPACE -Drng_test -I. -I. -I/home/ns235/ns-allinone-2.35/tclcl-1.20 -I/home/ns235/ns-allinone-2.35/otcl -I/home/ns235/ns-allinone-2.35/include -I/home/ns235/ns-allinone-2.35/include -I/home/ns235/ns-allinone-2.35/include -I/usr/include/pcap -I./tcp -I./sctp -I./common -I./link -I./queue -I./adc -I./apps -I./mac -I./mobile -I./trace -I./routing -I./tools -I./classifier -I./mcast -I./diffusion3/lib/main -I./diffusion3/lib -I./diffusion3/lib/nr -I./diffusion3/ns -I./diffusion3/filter_core -I./asim/ -I./qs -I./diffserv -I./satellite -I./wpan -o tcp/tcp-fack.o tcp/tcp-fack.cc
In file included from /usr/include/c++/4.6/vector:61:0,
from tcp/tcp-fack.cc:28:
/usr/include/c++/4.6/bits/stl_algobase.h: In function ‘const _Tp& std::max(const _Tp&, const _Tp&) [with _Tp = TracedInt]:
tcp/tcp-fack.cc:87:37: instantiated from here
/usr/include/c++/4.6/bits/stl_algobase.h:215:7: error: no match for ‘operator<’ in ‘__a < __b’
/usr/include/c++/4.6/bits/stl_algobase.h:215:7: note: candidates are:
/usr/include/c++/4.6/bits/stl_algobase.h:215:7: note: operator<(int, int) <built-in>
/usr/include/c++/4.6/bits/stl_algobase.h:215:7: note: no known conversion for argument 2 from ‘const TracedInt’ to ‘int’
/usr/include/c++/4.6/bits/stl_pair.h:207:5: note: template<class _T1, class _T2> bool std::operator<(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)
/usr/include/c++/4.6/bits/stl_iterator.h:291:5: note: template<class _Iterator> bool std::operator<(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)
/usr/include/c++/4.6/bits/stl_iterator.h:341:5: note: template<class _IteratorL, class _IteratorR> bool std::operator<(const std::reverse_iterator<_IteratorL>&, const std::reverse_iterator<_IteratorR>&)
/usr/include/c++/4.6/bits/stl_vector.h:1290:5: note: template<class _Tp, class _Alloc> bool std::operator<(const std::vector<_Tp, _Alloc>&, const std::vector<_Tp, _Alloc>&)
make: *** [tcp/tcp-fack.o] Error 1
here is a snippet of stl_algobase.h lines (197-218). Is using templete in tcp-fack.cc causing the error? (http://www.cs.rit.edu/usr/local/pub/swm/gcc-4.7.2/include/c++/4.7.2/bits/stl_algobase.h)
/**
* #brief This does what you think it does.
* #ingroup sorting_algorithms
* #param __a A thing of arbitrary type.
* #param __b Another thing of arbitrary type.
* #return The greater of the parameters.
*
* This is the simple classic generic implementation. It will work on
* temporary expressions, since they are only evaluated once, unlike a
* preprocessor macro.
*/
template<typename _Tp>
inline const _Tp&
max(const _Tp& __a, const _Tp& __b)
{
// concept requirements
__glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
//return __a < __b ? __b : __a;
if (__a < __b)
return __b;
return __a;
}
below is a snippet of tcp-fack.cc.Here is the original file with line number https://github.com/hbatmit/ns2.35/blob/master/tcp/tcp-fack.cc ( I only added <vector> at line 28)
#ifndef lint
static const char rcsid[] =
"#(#) /home/ctk21/cvsroot//hssstcp/ns/ns-2.1b9/tcp/tcp-fack.cc,v 1.2 2002/08/12 10:43:58 ctk21 Exp (PSC)";
#endif
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <vector> //line 28
#include "ip.h"
#include "tcp.h"
#include "flags.h"
#include "scoreboard.h"
#include "random.h"
#include "tcp-fack.h"
#include "template.h"
static class FackTcpClass : public TclClass {
public:
FackTcpClass() : TclClass("Agent/TCP/Fack") {}
TclObject* create(int, const char*const*) {
return (new FackTcpAgent());
}
} class_fack;
/*
* Process a dupack.
*/
void FackTcpAgent::oldack(Packet* pkt)
{
hdr_tcp *tcph = hdr_tcp::access(pkt);
last_ack_ = tcph->seqno();
highest_ack_ = last_ack_;
fack_ = max(fack_,highest_ack_);
/*
* There are conditions under which certain versions of TCP (e.g., tcp-fs)
* retract maxseq_. The following line of code helps in those cases. For
* versions of TCP, it is a NOP.
*/
maxseq_ = max(maxseq_, highest_ack_);// This is line 87
if (t_seqno_ < last_ack_ + 1)
t_seqno_ = last_ack_ + 1; // This is line 89
newtimer(pkt);
if (rtt_active_ && tcph->seqno() >= rtt_seq_) {
rtt_active_ = 0;
t_backoff_ = 1;
}
/* with timestamp option */
double tao = Scheduler::instance().clock() - tcph->ts_echo();
rtt_update(tao);
if (ts_resetRTO_) {
// From Andrei Gurtov
// FACK has not been updated to make sure the ECN works
// correctly in this case - Sally.
t_backoff_ = 1;
}
/* update average window */
awnd_ *= 1.0 - wnd_th_;
awnd_ += wnd_th_ * cwnd_;
/* if the connection is done, call finish() */
if ((highest_ack_ >= curseq_-1) && !closed_) {
closed_ = 1;
finish();
}
}
int FackTcpAgent::maxsack(Packet *pkt)
{
hdr_tcp *tcph = hdr_tcp::access(pkt);
int maxsack=-1, sack_index;
for (sack_index=0; sack_index < tcph->sa_length(); sack_index++) {
if (tcph->sa_right(sack_index) > maxsack)
maxsack = tcph->sa_right(sack_index);
}
return (maxsack-1);
}
** I have replaced <vector> with <map> and it showed the following error **
g++ -c -Wall -Wno-write-strings -DTCP_DELAY_BIND_ALL -DNO_TK -DTCLCL_CLASSINSTVAR -DNDEBUG -DLINUX_TCP_HEADER -DUSE_SHM -DHAVE_LIBTCLCL -DHAVE_TCLCL_H -DHAVE_LIBOTCL1_14 -DHAVE_OTCL_H -DHAVE_LIBTK8_5 -DHAVE_TK_H -DHAVE_LIBTCL8_5 -DHAVE_TCLINT_H -DHAVE_TCL_H -DHAVE_CONFIG_H -DNS_DIFFUSION -DSMAC_NO_SYNC -DCPP_NAMESPACE=std -DUSE_SINGLE_ADDRESS_SPACE -Drng_test -I. -I. -I/home/ns235/ns-allinone-2.35/tclcl-1.20 -I/home/ns235/ns-allinone-2.35/otcl -I/home/ns235/ns-allinone-2.35/include -I/home/ns235/ns-allinone-2.35/include -I/home/ns235/ns-allinone-2.35/include -I/usr/include/pcap -I./tcp -I./sctp -I./common -I./link -I./queue -I./adc -I./apps -I./mac -I./mobile -I./trace -I./routing -I./tools -I./classifier -I./mcast -I./diffusion3/lib/main -I./diffusion3/lib -I./diffusion3/lib/nr -I./diffusion3/ns -I./diffusion3/filter_core -I./asim/ -I./qs -I./diffserv -I./satellite -I./wpan -o tcp/tcp-fack.o tcp/tcp-fack.cc
In file included from /usr/include/c++/4.6/bits/stl_tree.h:63:0,
from /usr/include/c++/4.6/map:60,
from tcp/tcp-fack.cc:28:
/usr/include/c++/4.6/bits/stl_algobase.h: In function ‘const _Tp& std::max(const _Tp&, const _Tp&) [with _Tp = TracedInt]’:
tcp/tcp-fack.cc:87:37: instantiated from here
/usr/include/c++/4.6/bits/stl_algobase.h:215:7: error: no match for ‘operator<’ in ‘__a < __b’
/usr/include/c++/4.6/bits/stl_algobase.h:215:7: note: candidates are:
/usr/include/c++/4.6/bits/stl_algobase.h:215:7: note: operator<(int, int) <built-in>
/usr/include/c++/4.6/bits/stl_algobase.h:215:7: note: no known conversion for argument 2 from ‘const TracedInt’ to ‘int’
/usr/include/c++/4.6/bits/stl_pair.h:207:5: note: template<class _T1, class _T2> bool std::operator<(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)
/usr/include/c++/4.6/bits/stl_iterator.h:291:5: note: template<class _Iterator> bool std::operator<(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)
/usr/include/c++/4.6/bits/stl_iterator.h:341:5: note: template<class _IteratorL, class _IteratorR> bool std::operator<(const std::reverse_iterator<_IteratorL>&, const std::reverse_iterator<_IteratorR>&)
/usr/include/c++/4.6/bits/stl_tree.h:866:5: note: template<class _Key, class _Val, class _KeyOfValue, class _Compare, class _Alloc> bool std::operator<(const std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&, const std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&)
/usr/include/c++/4.6/bits/stl_map.h:899:5: note: template<class _Key, class _Tp, class _Compare, class _Alloc> bool std::operator<(const std::map<_Key, _Tp, _Compare, _Alloc>&, const std::map<_Key, _Tp, _Compare, _Alloc>&)
/usr/include/c++/4.6/bits/stl_multimap.h:817:5: note: template<class _Key, class _Tp, class _Compare, class _Alloc> bool std::operator<(const std::multimap<_Key, _Tp, _Compare, _Alloc>&, const std::multimap<_Key, _Tp, _Compare, _Alloc>&)
make: *** [tcp/tcp-fack.o] Error 1

I found the solution;
NS-2 template.h has swap() and max() functions. swap() is a member function of standard <vector>. and max() is a member function of standard stl_algorithm(). Also NS-2 god.h has a class called vector(). These NS-2 function were causing conflicts with <Vector> and <algorithm> libraries.
I changed the names of NS2 funtions and now everything works.
Thanks

Related

Unable to insert smart pointer into map

I am trying to insert my smart pointer, p_pointer<foo> pointing towards a foo class into map, however the program would not compile. Therefore I tried to use a normal pointer foo* and the program does compile. So far, I have been able to use the p_pointer like a normal pointer without any issue so I am surprised that this does not work. If anyone can explain to me why this would not work...
#include <iostream>
#include<map>
#include<string>
using namespace std;
template <class T>
class p_pointer
{
public:
T* cp;
size_t* refptr;
size_t* counter()
{
return refptr;
}
//default constructor
p_pointer():cp(0),refptr(new size_t(1)) {}
p_pointer(T*t):cp(t),refptr(new size_t(1)) {}
//copy constructor
p_pointer (const p_pointer&s):cp(s.cp),refptr(s.refptr)
{
refptr=s.refptr;
cp=s.cp;
*refptr=*refptr+1;
}
//destructor
~p_pointer()
{
if(--*refptr==0)
{
delete cp;
delete refptr;
}
}
//assignment operator
p_pointer&operator=(const p_pointer&s)
{
++*s.refptr;
//freeing the left hand size if it is the last one
if(--*refptr==0)
{
delete cp;
delete refptr;
}
cp=s.cp;
refptr=s.refptr;
}
operator bool()
{
return cp;
}
T*&operator->()
{
if(cp)
return cp;
else throw std::runtime_error("uninitialized player");
}
T operator*()
{
if(cp)
return *cp;
else throw std::runtime_error("uninitialized player");
}
};
class foo
{};
Method 1 (works)
int main()
{
map<foo*,int> x;
foo* y=new foo();
x[y]=1;
}
Method 2 (does not work)
int main()
{
map<p_pointer<foo>,int> x;
p_pointer<foo> y=new foo();
x[y]=1;
}
The error message is:
This is my first time posting an error message. I just copied all the content from build messages and paste it into here. Please let me know if this is not the optimal way
||=== Build: Debug in trial 821 (compiler: GNU GCC Compiler) ===|
C:\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_function.h||In instantiation of 'bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = p_pointer<foo>]':|
C:\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_map.h|498|required from 'std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const key_type&) [with _Key = p_pointer<foo>; _Tp = int; _Compare = std::less<p_pointer<foo> >; _Alloc = std::allocator<std::pair<const p_pointer<foo>, int> >; std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = int; std::map<_Key, _Tp, _Compare, _Alloc>::key_type = p_pointer<foo>]'|
C:\trial 821\main.cpp|80|required from here|
C:\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_function.h|371|error: no match for 'operator<' (operand types are 'const p_pointer<foo>' and 'const p_pointer<foo>')|
C:\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_function.h|371|note: candidates are:|
C:\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_function.h|371|note: operator<(int, int) <built-in>|
C:\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_function.h|371|note: no known conversion for argument 2 from 'const p_pointer<foo>' to 'int'|
C:\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_pair.h|220|note: template<class _T1, class _T2> constexpr bool std::operator<(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)|
C:\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_pair.h|220|note: template argument deduction/substitution failed:|
C:\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_function.h|371|note: 'const p_pointer<foo>' is not derived from 'const std::pair<_T1, _T2>'|
C:\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_iterator.h|298|note: template<class _Iterator> bool std::operator<(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)|
C:\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_iterator.h|298|note: template argument deduction/substitution failed:|
C:\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_function.h|371|note: 'const p_pointer<foo>' is not derived from 'const std::reverse_iterator<_Iterator>'|
C:\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_iterator.h|348|note: template<class _IteratorL, class _IteratorR> bool std::operator<(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_IteratorR>&)|
C:\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_iterator.h|348|note: template argument deduction/substitution failed:|
C:\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_function.h|371|note: 'const p_pointer<foo>' is not derived from 'const std::reverse_iterator<_Iterator>'|
C:\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_iterator.h|1072|note: template<class _IteratorL, class _IteratorR> bool std::operator<(const std::move_iterator<_Iterator>&, const std::move_iterator<_IteratorR>&)|
C:\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_iterator.h|1072|note: template argument deduction/substitution failed:|
C:\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_function.h|371|note: 'const p_pointer<foo>' is not derived from 'const std::move_iterator<_Iterator>'|
C:\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_iterator.h|1078|note: template<class _Iterator> bool std::operator<(const std::move_iterator<_Iterator>&, const std::move_iterator<_Iterator>&)|
C:\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_iterator.h|1078|note: template argument deduction/substitution failed:|
C:\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_function.h|371|note: 'const p_pointer<foo>' is not derived from 'const std::move_iterator<_Iterator>'|
C:\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\bits\basic_string.h|2588|note: template<class _CharT, class _Traits, class _Alloc> bool std::operator<(const std::basic_string<_CharT, _Traits, _Alloc>&, const std::basic_string<_CharT, _Traits, _Alloc>&)|
C:\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\bits\basic_string.h|2588|note: template argument deduction/substitution failed:|
C:\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_function.h|371|note: 'const p_pointer<foo>' is not derived from 'const std::basic_string<_CharT, _Traits, _Alloc>'|
C:\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\bits\basic_string.h|2600|note: template<class _CharT, class _Traits, class _Alloc> bool std::operator<(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*)|
C:\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\bits\basic_string.h|2600|note: template argument deduction/substitution failed:|
C:\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_function.h|371|note: 'const p_pointer<foo>' is not derived from 'const std::basic_string<_CharT, _Traits, _Alloc>'|
C:\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\bits\basic_string.h|2612|note: template<class _CharT, class _Traits, class _Alloc> bool std::operator<(const _CharT*, const std::basic_string<_CharT, _Traits, _Alloc>&)|
C:\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\bits\basic_string.h|2612|note: template argument deduction/substitution failed:|
C:\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_function.h|371|note: mismatched types 'const _CharT*' and 'p_pointer<foo>'|
C:\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_tree.h|980|note: template<class _Key, class _Val, class _KeyOfValue, class _Compare, class _Alloc> bool std::operator<(const std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&, const std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&)|
C:\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_tree.h|980|note: template argument deduction/substitution failed:|
C:\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_function.h|371|note: 'const p_pointer<foo>' is not derived from 'const std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>'|
C:\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\array|242|note: template<class _Tp, unsigned int _Nm> bool std::operator<(const std::array<_Tp, _Nm>&, const std::array<_Tp, _Nm>&)|
C:\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\array|242|note: template argument deduction/substitution failed:|
C:\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_function.h|371|note: 'const p_pointer<foo>' is not derived from 'const std::array<_Tp, _Nm>'|
C:\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\tuple|857|note: template<class ... _TElements, class ... _UElements> constexpr bool std::operator<(const std::tuple<_Args1 ...>&, const std::tuple<_Args2 ...>&)|
C:\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\tuple|857|note: template argument deduction/substitution failed:|
C:\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_function.h|371|note: 'const p_pointer<foo>' is not derived from 'const std::tuple<_Args1 ...>'|
C:\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_map.h|1017|note: template<class _Key, class _Tp, class _Compare, class _Alloc> bool std::operator<(const std::map<_Key, _Tp, _Compare, _Alloc>&, const std::map<_Key, _Tp, _Compare, _Alloc>&)|
C:\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_map.h|1017|note: template argument deduction/substitution failed:|
C:\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_function.h|371|note: 'const p_pointer<foo>' is not derived from 'const std::map<_Key, _Tp, _Compare, _Alloc>'|
C:\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_multimap.h|920|note: template<class _Key, class _Tp, class _Compare, class _Alloc> bool std::operator<(const std::multimap<_Key, _Tp, _Compare, _Alloc>&, const std::multimap<_Key, _Tp, _Compare, _Alloc>&)|
C:\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_multimap.h|920|note: template argument deduction/substitution failed:|
C:\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_function.h|371|note: 'const p_pointer<foo>' is not derived from 'const std::multimap<_Key, _Tp, _Compare, _Alloc>'|
||=== Build failed: 1 error(s), 3 warning(s) (0 minute(s), 0 second(s)) ===|
Your problem can be found in the error message: no match for 'operator<' (operand types are 'const p_pointer' and 'const p_pointer')|. In order to use map with a custom type (p_pointer in this case), that type needs to declare a comparison operator < which is used to sort the keys in the map. You'll need to implement the < operator, and that should fix this error. (Other errors may show up to replace it though.)
Again, I would recommend just using the existing std::shared_ptr rather than re-implementing it yourself, unless you have some reason not to do so.

std::set.insert won't compile with custom class [duplicate]

This question already has answers here:
problems with c++ set container
(2 answers)
Closed 9 years ago.
I have a class, Record, with three private integer fields, getters and setters, and a default and specific constructor. I intend to populate a set with Records, but am having issues getting the code to work.
#include <set>
using namespace std;
class Record
{
int a, b, c;
public:
//getters and setters
Record(){a = -1; b = -1; c = -1;};
}
int main()
{
set< Record > s;
s.insert(Record());
}
Attempting to compile results in this error:
C:\Users\Denton\Documents\Indiana University\Class Documents\Spring
2013\CSCI-H2
12\Assignment9>g++ a9.cpp -o a9
In file included from c:\mingw\bin../lib/gcc/mingw32/4.6.2/include/c++/string:5
0:0,
from c:\mingw\bin../lib/gcc/mingw32/4.6.2/include/c++/bits/loc
ale_classes.h:42,
from c:\mingw\bin../lib/gcc/mingw32/4.6.2/include/c++/bits/ios
_base.h:43,
from c:\mingw\bin../lib/gcc/mingw32/4.6.2/include/c++/ios:43,
from c:\mingw\bin../lib/gcc/mingw32/4.6.2/include/c++/ostream:
40,
from c:\mingw\bin../lib/gcc/mingw32/4.6.2/include/c++/iostream
:40,
from a9.cpp:3:
c:\mingw\bin../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_function.h:
In member
function 'bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _
Tp = Record]':
c:\mingw\bin../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_tree.h:1267:4:
inst
antiated from 'std::pair, bool> std::_Rb_tree<_Key,
_Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(const _Val&) [with _Key
= Record, _Val = Record, _KeyOfValue = std::_Identity, _Compare = std::l
ess, _Alloc = std::allocator]'
c:\mingw\bin../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_set.h:410:29:
insta
ntiated from 'std::pair,
_Compare, typename _Alloc::rebind<_Key>::other>::const_iterator, bool> std::set
<_Key, _Compare, _Alloc>::insert(const value_type&) [with _Key = Record, _Compar
e = std::less, _Alloc = std::allocator, typename std::_Rb_tree<_
Key, _Key, std::_Identity<_Key>, _Compare, typename _Alloc::rebind<_Key>::other>
::const_iterator = std::_Rb_tree_const_iterator, std::set<_Key, _Compare
, _Alloc>::value_type = Record]'
a9.cpp:72:28: instantiated from here
c:\mingw\bin../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_function.h:236:22:
er
ror: no match for 'operator<' in '__x < __y'
c:\mingw\bin../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_function.h:236:22:
no
te: candidates are:
c:\mingw\bin../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_pair.h:207:5:
note: t
emplate bool std::operator<(const std::pair<_T1, _T2>&, co
nst std::pair<_T1, _T2>&)
c:\mingw\bin../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_iterator.h:291:5:
not
e: template bool std::operator<(const std::reverse_iterator<_It
erator>&, const std::reverse_iterator<_Iterator>&)
c:\mingw\bin../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_iterator.h:341:5:
not
e: template bool std::operator<(const std::r
everse_iterator<_IteratorL>&, const std::reverse_iterator<_IteratorR>&)
c:\mingw\bin../lib/gcc/mingw32/4.6.2/include/c++/bits/basic_string.h:2510:5:
no
te: template bool std::operator<(cons
t std::basic_string<_CharT, _Traits, _Alloc>&, const std::basic_string<_CharT, _
Traits, _Alloc>&)
c:\mingw\bin../lib/gcc/mingw32/4.6.2/include/c++/bits/basic_string.h:2522:5:
no
te: template bool std::operator<(cons
t std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*)
c:\mingw\bin../lib/gcc/mingw32/4.6.2/include/c++/bits/basic_string.h:2534:5:
no
te: template bool std::operator<(cons
t _CharT*, const std::basic_string<_CharT, _Traits, _Alloc>&)
c:\mingw\bin../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_vector.h:1290:5:
note
: template bool std::operator<(const std::vector<_Tp, _
Alloc>&, const std::vector<_Tp, _Alloc>&)
c:\mingw\bin../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_list.h:1593:5:
note:
template bool std::operator<(const std::list<_Tp, _Allo
c>&, const std::list<_Tp, _Alloc>&)
c:\mingw\bin../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_tree.h:856:5:
note: t
emplate
bool std::operator<(const std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _All
oc>&, const std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&)
c:\mingw\bin../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_set.h:713:5:
note: te
mplate bool std::operator<(const std::
set<_Key, _Compare, _Alloc>&, const std::set<_Key, _Compare, _Alloc>&)
c:\mingw\bin../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_multiset.h:696:5:
not
e: template bool std::operator<(const
std::multiset<_Key, _Compare, _Alloc>&, const std::multiset<_Key, _Compare, _All
oc>&)
insert works fine when I make a set s and s.insert(1)
What needs to be done for this code to compile?
Thanks in advance.
You need to overload the operator< :
something like the following:
bool operator<(const Record& rhs) const
{
return a < rhs.a; //assume that you compare the record based on a
}
The reason is that STL set maintains order on elements. std::set supports specifying a comparison function. The default is less which will use operator < to check equality.
Meanwhile, you have to end you class definition with ; otherwise, compile error.
class Record
{
int a, b, c;
public:
//getters and setters
Record(){a = -1; b = -1; c = -1;};
}; //<---Cannot miss this ;

using the find function from algorithm on vector

I want to use the find function from the algorithm library on a vector object. The vector object is a vector of customers, (Customer is a class I made). I first ran it and it gave me an error in stl_algo.h. I search the web for it and I searched here for it too, I found a question here about it and I ran the same code, but I still got that error.
My code is here:
Header File:
#include <string>
#include <sstream>
#include <map>
#include <vector>
using namespace std;
enum Status {ACTIVE, INACTIVE};
class Customer {
private:
// ID Database class for storing customers' ids
class IdDB {
private:
friend class Customer;
// member field
static map <string, int> idList;
// member function
static int getNumber (const string &threeLetters) {
map<string, int>::iterator i = idList.find(threeLetters);
if (i == idList.end()) {
idList.insert(pair <string, int> (threeLetters, 0));
return 0;
}else{
return ++(i->second);
}
}
};
string id;
string name;
string address;
Status status;
void makeId () {
string threeLetters = name.substr(0, 3);
int idNum = IdDB::getNumber(threeLetters);
stringstream oss;
oss << threeLetters << idNum;
id = oss.str();
}
public:
Customer (const string&, const string&, const Status);
// Accessor Methods
string &getId ();
string &getName ();
string &getAddress ();
Status getStatus ();
// Mutator Methods
void setAddress (const string&);
void setStatus (const Status);
// Misc. Methods
void printStatus ();
// Equality Operator Overloading
friend bool operator == (Customer&, Customer&);
};
class CustomerDB {
private:
static vector<Customer> customersList;
public:
static void addCustomer (const Customer&);
static void deleteCustomer (Customer&);
};
Source Code:
#include <iostream>
#include <string>
#include <algorithm>
#include "Customer.h"
using namespace std;
map<string, int> Customer::IdDB::idList;
Customer::Customer (const string &cName, const string &cAddress, const Status cStatus) : name(cName), address(cAddress), status(cStatus) {
makeId();
}
// Accessor Methods
string &Customer::getId () { return id; }
string &Customer::getName () { return name; }
string &Customer::getAddress () { return address; }
Status Customer::getStatus () { return status; }
// Mutator Methods
void Customer::setAddress (const string &newAddress) { address = newAddress; }
void Customer::setStatus (const Status newStatus) { status = newStatus; }
// Misc. Methods
void Customer::printStatus () {
if (status == ACTIVE)
cout << "Active";
else
cout << "In-Active";
}
vector<Customer> CustomerDB::customersList;
void CustomerDB::addCustomer (const Customer &customer) {
customersList.push_back(customer);
}
void CustomerDB::deleteCustomer (Customer &customer) {
vector<Customer>::iterator i;
i = find(customersList.begin(), customersList.end(), customer); // getting error in here
}
// Equality Operator Overloading
bool operator == (Customer &cust1, Customer &cust2) {
return cust1.getId() == cust2.getId();
}
after building with Code::Blocks, I got this,
in header file stl_algo.h:
}
/// This is an overload used by find() for the RAI case.
template<typename _RandomAccessIterator, typename _Tp>
_RandomAccessIterator
__find(_RandomAccessIterator __first, _RandomAccessIterator __last,
const _Tp& __val, random_access_iterator_tag)
{
typename iterator_traits<_RandomAccessIterator>::difference_type
__trip_count = (__last - __first) >> 2;
for (; __trip_count > 0; --__trip_count)
{
if (*__first == __val) // error in here exactly getting a red block
return __first;
++__first;
if (*__first == __val)
return __first;
++__first;
if (*__first == __val)
return __first;
Thanks
EDIT: Here is the build log
Compiling: C:\Users\KiKo-SaMa\Desktop\C++\DVD_App\Customer.cpp
In file included from c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/algorithm:63:0,
from C:\Users\KiKo-SaMa\Desktop\C++\DVD_App\Customer.cpp:5:
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_algo.h: In function '_RandomAccessIterator std::__find(_RandomAccessIterator, _RandomAccessIterator, const _Tp&, std::random_access_iterator_tag) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Customer*, std::vector<Customer> >, _Tp = Customer]':
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_algo.h:4403:45: instantiated from '_IIter std::find(_IIter, _IIter, const _Tp&) [with _IIter = __gnu_cxx::__normal_iterator<Customer*, std::vector<Customer> >, _Tp = Customer]'
C:\Users\KiKo-SaMa\Desktop\C++\DVD_App\Customer.cpp:42:66: instantiated from here
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_algo.h:162:4: error: no match for 'operator==' in '__first.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = Customer*, _Container = std::vector<Customer>, __gnu_cxx::__normal_iterator<_Iterator, _Container>::reference = Customer&]() == __val'
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_algo.h:162:4: note: candidates are:
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/postypes.h:218:5: note: template<class _StateT> bool std::operator==(const std::fpos<_StateT>&, const std::fpos<_StateT>&)
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_pair.h:201:5: note: template<class _T1, class _T2> bool std::operator==(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_iterator.h:285:5: note: template<class _Iterator> bool std::operator==(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_iterator.h:335:5: note: template<class _IteratorL, class _IteratorR> bool std::operator==(const std::reverse_iterator<_IteratorL>&, const std::reverse_iterator<_IteratorR>&)
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/allocator.h:122:5: note: template<class _T1, class _T2> bool std::operator==(const std::allocator<_T1>&, const std::allocator<_T2>&)
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/allocator.h:127:5: note: template<class _Tp> bool std::operator==(const std::allocator<_Tp1>&, const std::allocator<_Tp1>&)
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/basic_string.h:2427:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator==(const std::basic_string<_CharT, _Traits, _Alloc>&, const std::basic_string<_CharT, _Traits, _Alloc>&)
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/basic_string.h:2434:5: note: template<class _CharT> typename __gnu_cxx::__enable_if<std::__is_char<_Tp>::__value, bool>::__type std::operator==(const std::basic_string<_CharT>&, const std::basic_string<_CharT>&)
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/basic_string.h:2448:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator==(const _CharT*, const std::basic_string<_CharT, _Traits, _Alloc>&)
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/basic_string.h:2460:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator==(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*)
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/streambuf_iterator.h:194:5: note: template<class _CharT, class _Traits> bool std::operator==(const std::istreambuf_iterator<_CharT, _Traits>&, const std::istreambuf_iterator<_CharT, _Traits>&)
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_tree.h:309:5: note: template<class _Val> bool std::operator==(const std::_Rb_tree_iterator<_Tp>&, const std::_Rb_tree_const_iterator<_Val>&)
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_tree.h:846:5: note: template<class _Key, class _Val, class _KeyOfValue, class _Compare, class _Alloc> bool std::operator==(const std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&, const std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&)
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_map.h:877:5: note: template<class _Key, class _Tp, class _Compare, class _Alloc> bool std::operator==(const std::map<_Key, _Tp, _Compare, _Alloc>&, const std::map<_Key, _Tp, _Compare, _Alloc>&)
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_multimap.h:795:5: note: template<class _Key, class _Tp, class _Compare, class _Alloc> bool std::operator==(const std::multimap<_Key, _Tp, _Compare, _Alloc>&, const std::multimap<_Key, _Tp, _Compare, _Alloc>&)
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_vector.h:1273:5: note: template<class _Tp, class _Alloc> bool std::operator==(const std::vector<_Tp, _Alloc>&, const std::vector<_Tp, _Alloc>&)
C:\Users\KiKo-SaMa\Desktop\C++\DVD_App\Customer.cpp:46:6: note: bool operator==(Customer&, Customer&)
C:\Users\KiKo-SaMa\Desktop\C++\DVD_App\Customer.cpp:46:6: note: no known conversion for argument 2 from 'const Customer' to 'Customer&'
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_algo.h:4403:45: instantiated from '_IIter std::find(_IIter, _IIter, const _Tp&) [with _IIter = __gnu_cxx::__normal_iterator<Customer*, std::vector<Customer> >, _Tp = Customer]'
C:\Users\KiKo-SaMa\Desktop\C++\DVD_App\Customer.cpp:42:66: instantiated from here
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_algo.h:166:4: error: no match for 'operator==' in '__first.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = Customer*, _Container = std::vector<Customer>, __gnu_cxx::__normal_iterator<_Iterator, _Container>::reference = Customer&]() == __val'
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_algo.h:166:4: note: candidates are:
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/postypes.h:218:5: note: template<class _StateT> bool std::operator==(const std::fpos<_StateT>&, const std::fpos<_StateT>&)
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_pair.h:201:5: note: template<class _T1, class _T2> bool std::operator==(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_iterator.h:285:5: note: template<class _Iterator> bool std::operator==(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_iterator.h:335:5: note: template<class _IteratorL, class _IteratorR> bool std::operator==(const std::reverse_iterator<_IteratorL>&, const std::reverse_iterator<_IteratorR>&)
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/allocator.h:122:5: note: template<class _T1, class _T2> bool std::operator==(const std::allocator<_T1>&, const std::allocator<_T2>&)
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/allocator.h:127:5: note: template<class _Tp> bool std::operator==(const std::allocator<_Tp1>&, const std::allocator<_Tp1>&)
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/basic_string.h:2427:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator==(const std::basic_string<_CharT, _Traits, _Alloc>&, const std::basic_string<_CharT, _Traits, _Alloc>&)
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/basic_string.h:2434:5: note: template<class _CharT> typename __gnu_cxx::__enable_if<std::__is_char<_Tp>::__value, bool>::__type std::operator==(const std::basic_string<_CharT>&, const std::basic_string<_CharT>&)
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/basic_string.h:2448:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator==(const _CharT*, const std::basic_string<_CharT, _Traits, _Alloc>&)
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/basic_string.h:2460:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator==(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*)
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/streambuf_iterator.h:194:5: note: template<class _CharT, class _Traits> bool std::operator==(const std::istreambuf_iterator<_CharT, _Traits>&, const std::istreambuf_iterator<_CharT, _Traits>&)
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_tree.h:309:5: note: template<class _Val> bool std::operator==(const std::_Rb_tree_iterator<_Tp>&, const std::_Rb_tree_const_iterator<_Val>&)
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_tree.h:846:5: note: template<class _Key, class _Val, class _KeyOfValue, class _Compare, class _Alloc> bool std::operator==(const std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&, const std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&)
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_map.h:877:5: note: template<class _Key, class _Tp, class _Compare, class _Alloc> bool std::operator==(const std::map<_Key, _Tp, _Compare, _Alloc>&, const std::map<_Key, _Tp, _Compare, _Alloc>&)
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_multimap.h:795:5: note: template<class _Key, class _Tp, class _Compare, class _Alloc> bool std::operator==(const std::multimap<_Key, _Tp, _Compare, _Alloc>&, const std::multimap<_Key, _Tp, _Compare, _Alloc>&)
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_vector.h:1273:5: note: template<class _Tp, class _Alloc> bool std::operator==(const std::vector<_Tp, _Alloc>&, const std::vector<_Tp, _Alloc>&)
C:\Users\KiKo-SaMa\Desktop\C++\DVD_App\Customer.cpp:46:6: note: bool operator==(Customer&, Customer&)
C:\Users\KiKo-SaMa\Desktop\C++\DVD_App\Customer.cpp:46:6: note: no known conversion for argument 2 from 'const Customer' to 'Customer&'
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_algo.h:170:4: error: no match for 'operator==' in '__first.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = Customer*, _Container = std::vector<Customer>, __gnu_cxx::__normal_iterator<_Iterator, _Container>::reference = Customer&]() == __val'
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_algo.h:170:4: note: candidates are:
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/postypes.h:218:5: note: template<class _StateT> bool std::operator==(const std::fpos<_StateT>&, const std::fpos<_StateT>&)
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_pair.h:201:5: note: template<class _T1, class _T2> bool std::operator==(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_iterator.h:285:5: note: template<class _Iterator> bool std::operator==(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_iterator.h:335:5: note: template<class _IteratorL, class _IteratorR> bool std::operator==(const std::reverse_iterator<_IteratorL>&, const std::reverse_iterator<_IteratorR>&)
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/allocator.h:122:5: note: template<class _T1, class _T2> bool std::operator==(const std::allocator<_T1>&, const std::allocator<_T2>&)
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/allocator.h:127:5: note: template<class _Tp> bool std::operator==(const std::allocator<_Tp1>&, const std::allocator<_Tp1>&)
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/basic_string.h:2427:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator==(const std::basic_string<_CharT, _Traits, _Alloc>&, const std::basic_string<_CharT, _Traits, _Alloc>&)
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/basic_string.h:2434:5: note: template<class _CharT> typename __gnu_cxx::__enable_if<std::__is_char<_Tp>::__value, bool>::__type std::operator==(const std::basic_string<_CharT>&, const std::basic_string<_CharT>&)
Process terminated with status 1 (0 minutes, 1 seconds)
50 errors, 0 warnings
You should work on your const correctness, the problem is that your equality comparator takes the arguments by non const reference, but the last argument to find is taken by const reference, which means that the compiler cannot use it there.
Incidentally, once you add the const there you will be forced to add const accessors to the data. Also, if your operator only uses the public interface there is no need to declare it as a friend
CodeBlocks may give errors about the stl files however they are not actually the errors. It shows the related stl file of your error.

Problems using a map with a bitset as a key

I am trying to create a map in C++ with bitset as a key. However the compiler generates the following error messages
In file included from /usr/include/c++/4.6/string:50:0,
from /usr/include/c++/4.6/bits/locale_classes.h:42,
from /usr/include/c++/4.6/bits/ios_base.h:43,
from /usr/include/c++/4.6/ios:43,
from /usr/include/c++/4.6/ostream:40,
from /usr/include/c++/4.6/iostream:40,
from test2.cpp:1:
/usr/include/c++/4.6/bits/stl_function.h: In member function ‘bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = std::bitset<8u>]’:
/usr/include/c++/4.6/bits/stl_map.h:452:2: instantiated from ‘std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const key_type&) [with _Key = std::bitset<8u>, _Tp = int, _Compare = std::less<std::bitset<8u> >, _Alloc = std::allocator<std::pair<const std::bitset<8u>, int> >, std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = int, std::map<_Key, _Tp, _Compare, _Alloc>::key_type = std::bitset<8u>]’
test2.cpp:22:30: instantiated from here
/usr/include/c++/4.6/bits/stl_function.h:236:22: error: no match for ‘operator<’ in ‘__x < __y’
/usr/include/c++/4.6/bits/stl_function.h:236:22: note: candidates are:
/usr/include/c++/4.6/bits/stl_pair.h:207:5: note: template<class _T1, class _T2> bool std::operator<(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)
/usr/include/c++/4.6/bits/stl_iterator.h:291:5: note: template<class _Iterator> bool std::operator<(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)
/usr/include/c++/4.6/bits/stl_iterator.h:341:5: note: template<class _IteratorL, class _IteratorR> bool std::operator<(const std::reverse_iterator<_IteratorL>&, const std::reverse_iterator<_IteratorR>&)
/usr/include/c++/4.6/bits/basic_string.h:2510:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator<(const std::basic_string<_CharT, _Traits, _Alloc>&, const std::basic_string<_CharT, _Traits, _Alloc>&)
/usr/include/c++/4.6/bits/basic_string.h:2522:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator<(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*)
/usr/include/c++/4.6/bits/basic_string.h:2534:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator<(const _CharT*, const std::basic_string<_CharT, _Traits, _Alloc>&)
/usr/include/c++/4.6/bits/stl_tree.h:856:5: note: template<class _Key, class _Val, class _KeyOfValue, class _Compare, class _Alloc> bool std::operator<(const std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&, const std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&)
/usr/include/c++/4.6/bits/stl_set.h:713:5: note: template<class _Key, class _Compare, class _Alloc> bool std::operator<(const std::set<_Key, _Compare, _Alloc>&, const std::set<_Key, _Compare, _Alloc>&)
/usr/include/c++/4.6/bits/stl_multiset.h:696:5: note: template<class _Key, class _Compare, class _Alloc> bool std::operator<(const std::multiset<_Key, _Compare, _Alloc>&, const std::multiset<_Key, _Compare, _Alloc>&)
/usr/include/c++/4.6/bits/stl_map.h:894:5: note: template<class _Key, class _Tp, class _Compare, class _Alloc> bool std::operator<(const std::map<_Key, _Tp, _Compare, _Alloc>&, const std::map<_Key, _Tp, _Compare, _Alloc>&)
/usr/include/c++/4.6/bits/stl_multimap.h:812:5: note: template<class _Key, class _Tp, class _Compare, class _Alloc> bool std::operator<(const std::multimap<_Key, _Tp, _Compare, _Alloc>&, const std::multimap<_Key, _Tp, _Compare, _Alloc>&)
The progam code is given below
I am trying to use bitset as a key to a map in C++. However, everytime I run the code below I run into errros.
#include <iostream>
#include <algorithm>
#include <string>
#include <bitset>
#include <set>
#include <utility>
using namespace std;
int main(int argc, char *argv[])
{
bitset<8> test;
test = 9;
cout<<"Set to 9"<<endl;
map <bitset<8> , int> mymap;
pair <biset<8> , int> p;
p.first = test;
p.second = 9;
string teststring;
teststring = test.to_string<char,char_traits<char>,allocator<char> >();
cout<<teststring<<temymap[test]<<endl;
return 0;
}
Just use your own comparator class:
struct Comparer {
bool operator() (const bitset<8> &b1, const bitset<8> &b2) const {
return b1.to_ulong() < b2.to_ulong();
}
};
/* ... */
map <bitset<8> , int, Comparer> mymap;
Note that you can extend this solution to support arbitrary length bitsets, as long as they are small enough to be converted to an unsigned long:
template<size_t sz> struct bitset_comparer {
bool operator() (const bitset<sz> &b1, const bitset<sz> &b2) const {
return b1.to_ulong() < b2.to_ulong();
}
};
map <bitset<8> , int, bitset_comparer<8> > mymap;
map <bitset<16> , int, bitset_comparer<16> > mymap16;
An alternative solution would be to simply use an unordered_map, if this still meets your requirements.
This could be std::unordered_map<bitset<N>, T> or boost::unordered_map<bitset<N>, T>, depending on C++ version or performance considerations.
This avoids the need for comparison and may prove faster, depending on requirements.
You can define your comparison function. If you assume your bitset models an unsigned integral value then the following function will order the bitsets in increasing order (and works for any N).
template <size_t N>
class LessThan {
public:
bool operator() (const std::bitset<N> &lhs, const std::bitset<N> &rhs) const
{
size_t i = N;
while ( i > 0 ) {
if ( lhs[i-1] == rhs[i-1] ) {
i--;
} else if ( lhs[i-1] < rhs[i-1] ) {
return true;
} else {
return false;
}
}
return false;
}
};
If you run the following snippet:
const size_t mysz = 10;
std::map< std::bitset<mysz>, size_t, Less<mysz> > mymap;
for ( size_t i = 0; i < 10; i++ ) {
mymap.insert( std::make_pair(std::bitset<mysz>(i),i) );
}
You will have this map:
mymap[0] is the pair ((0,0,0,0,0,0,0,0,0,0), 0)
mymap[1] is the pair ((1,0,0,0,0,0,0,0,0,0), 1)
mymap[2] is the pair ((0,1,0,0,0,0,0,0,0,0), 2)
mymap[3] is the pair ((1,1,0,0,0,0,0,0,0,0), 3)
mymap[4] is the pair ((0,0,1,0,0,0,0,0,0,0), 4)
mymap[5] is the pair ((1,0,1,0,0,0,0,0,0,0), 5)
mymap[6] is the pair ((0,1,1,0,0,0,0,0,0,0), 6)
mymap[7] is the pair ((1,1,1,0,0,0,0,0,0,0), 7)
mymap[8] is the pair ((0,0,0,1,0,0,0,0,0,0), 8)
mymap[9] is the pair ((1,0,0,1,0,0,0,0,0,0), 9)
This will allow map<bitset<N>,int> stuff directly:
namespace std{
template<size_t N>
struct less<bitset<N> > : binary_function <bitset<N>,bitset<N>,bool>{
bool operator()(const bitset<N> &L, const bitset<N> &R) const{
for(unsigned int i=0;i<L.size();i++)
if(L.test(i)){
if(!R.test(i))return false;
}else{
if(R.test(i))return true;
}
return false; //same
}
};
}
For storing in the map you can convert bitset to string for large bitset if it's not convertible to u_long and for updating you can change back to bitset and do your changes and store back as a string.
map<string , int> mymap;
bitset<N> mybs("10100"); // converting string to bitset
map[mybs.to_string()] = 34; // bitset to string for map
This is valid as long as you don't care about the comparison.

C++ wait problem

I have a code like this
if (pid > 0) {
// Child
} else {
// Parent
}
while (wait() > 0) {}
And there are includes
#include <cstdlib>
#include <iostream>
#include <cstdio>
#include <ctime>
#include <sys/types.h>
But when i try to compiile it with g++ (g++ test.cpp -o test) a have an errors:
lab3.cpp: In function «int main(int, char**)»:
lab3.cpp:57:18: error: no match for «operator>» in «{0} > 0»
lab3.cpp:57:18: warning: candidates are:
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/bits/stl_pair.h:220:67: замечание: template<class _T1, class _T2> bool std::operator>(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/bits/stl_iterator.h:304:46: замечание: template<class _Iterator> bool std::operator>(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/bits/stl_iterator.h:354:47: замечание: template<class _IteratorL, class _IteratorR> bool std::operator>(const std::reverse_iterator<_IteratorL>&, const std::reverse_iterator<_IteratorR>&)
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/bits/basic_string.h:2548:58: замечание: template<class _CharT, class _Traits, class _Alloc> bool std::operator>(const std::basic_string<_CharT, _Traits, _Alloc>&, const std::basic_string<_CharT, _Traits, _Alloc>&)
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/bits/basic_string.h:2560:27: замечание: template<class _CharT, class _Traits, class _Alloc> bool std::operator>(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*)
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/bits/basic_string.h:2572:58: замечание: template<class _CharT, class _Traits, class _Alloc> bool std::operator>(const _CharT*, const std::basic_string<_CharT, _Traits, _Alloc>&)
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/bits/stl_vector.h:1303:77: замечание: template<class _Tp, class _Alloc> bool std::operator>(const std::vector<_Tp, _Alloc>&, const std::vector<_Tp, _Alloc>&)
What i'm doing wrong?
Your includes pull /usr/include/bits/waitstatus.h indirectly, which defines a union wait. Since you don't have a declaration of the wait-function, C++ sees wait() as a constructor expression. This means wait()>0 calls for an operator>(const wait &,int) which does not exist of course. GCC's diagnostics aren't really helpful here. Add sys/wait.h to fetch a valid prototype for the wait function.
wait() is defined in sys/wait.h.
But there's probably something else going on there that we can't see with the code you posted.
Try changing that to:
while (::wait() > 0) ...
to make sure you're calling the wait() function from the global namespace and not some other class or construct imported from elsewhere.