Link to home
Start Free TrialLog in
Avatar of FireBall
FireBall

asked on

Struct for map

Hello ,

I want to use given struct in a map as this : std::map<Web_Dumper, int> http_dumper;


but i am getting errors, where should i do mistake ?

struct Web_Dumper
{
    IP4 ip;
    const std::string Agent;
    const std::string Version;
    const std::string Host;
    const std::string Path;
    const std::string Ulke;
    long int Tarih;
    Web_Dumper(IP4  szip,const std::string sAgent,const std::string sVersion,const std::string sHost,const std::string sPath,const std::string sUlke,long int sTarih) : 
    ip(szip), Agent(sAgent) , Version(sVersion), Host(sHost), Path(sPath),Ulke(sUlke), Tarih(sTarih){}
    friend bool operator<(const Web_Dumper & ipt1, const Web_Dumper & ipt2) { return std::make_pair(ipt1.ip,ipt1.Agent,ipt1.Version,ipt1.Host,ipt1.Path,ipt1.Ulke,ipt1.Tarih) <  std::make_pair(ipt2.ip,ipt2.Agent,ipt2.Version,ipt2.Host,ipt2.Path,ipt2.Ulke,ipt2.Tarih); }
};

Open in new window




AnomalyDetector.c: In function ‘bool operator<(const Web_Dumper&, const Web_Dumper&)’:
AnomalyDetector.c:1311:173: error: no matching function for call to ‘make_pair(const IP4&, const string&, const string&, const string&, const string&, const string&, const long int&)’
     friend bool operator<(const Web_Dumper & ipt1, const Web_Dumper & ipt2) { return std::make_pair(ipt1.ip,ipt1.Agent,ipt1.Version,ipt1.Host,ipt1.Path,ipt1.Ulke,ipt1.Tarih)
                                                                                                                                                                             ^
AnomalyDetector.c:1311:173: note: candidate is:
In file included from /usr/include/c++/4.8.2/bits/stl_algobase.h:64:0,
                 from /usr/include/c++/4.8.2/bits/stl_tree.h:61,
                 from /usr/include/c++/4.8.2/map:60,
                 from AnomalyDetector.c:10:
/usr/include/c++/4.8.2/bits/stl_pair.h:276:5: note: template<class _T1, class _T2> constexpr std::pair<typename std::__decay_and_strip<_Tp>::__type, typename std::__decay_and_strip<_T2>::__type> std::make_pair(_T1&&, _T2&&)
     make_pair(_T1&& __x, _T2&& __y)
     ^
/usr/include/c++/4.8.2/bits/stl_pair.h:276:5: note:   template argument deduction/substitution failed:
AnomalyDetector.c:1311:173: note:   candidate expects 2 arguments, 7 provided
     friend bool operator<(const Web_Dumper & ipt1, const Web_Dumper & ipt2) { return std::make_pair(ipt1.ip,ipt1.Agent,ipt1.Version,ipt1.Host,ipt1.Path,ipt1.Ulke,ipt1.Tarih)
                                                                                                                                                                             ^
AnomalyDetector.c:1312:93: error: no matching function for call to ‘make_pair(const IP4&, const string&, const string&, const string&, const string&, const string&, const long int&)’
   <  std::make_pair(ipt2.ip,ipt2.Agent,ipt2.Version,ipt2.Host,ipt2.Path,ipt2.Ulke,ipt2.Tarih); }
                                                                                             ^
AnomalyDetector.c:1312:93: note: candidate is:
In file included from /usr/include/c++/4.8.2/bits/stl_algobase.h:64:0,
                 from /usr/include/c++/4.8.2/bits/stl_tree.h:61,
                 from /usr/include/c++/4.8.2/map:60,
                 from AnomalyDetector.c:10:
/usr/include/c++/4.8.2/bits/stl_pair.h:276:5: note: template<class _T1, class _T2> constexpr std::pair<typename std::__decay_and_strip<_Tp>::__type, typename std::__decay_and_strip<_T2>::__type> std::make_pair(_T1&&, _T2&&)
     make_pair(_T1&& __x, _T2&& __y)
     ^
/usr/include/c++/4.8.2/bits/stl_pair.h:276:5: note:   template argument deduction/substitution failed:
AnomalyDetector.c:1312:93: note:   candidate expects 2 arguments, 7 provided
   <  std::make_pair(ipt2.ip,ipt2.Agent,ipt2.Version,ipt2.Host,ipt2.Path,ipt2.Ulke,ipt2.Tarih); }
                                                                                             ^
AnomalyDetector.c: In function ‘void parse_header(void*)’:
AnomalyDetector.c:1360:80: error: no matching function for call to ‘Web_Dumper::Web_Dumper(std::string&, std::string&, std::string&, std::string&, std::string&, char*&, long int)’
     http_dumper[Web_Dumper(ip,ua,version,host,path,gir->country_code,Zaman_MS())];
                                                                                ^
AnomalyDetector.c:1360:80: note: candidates are:
AnomalyDetector.c:1309:5: note: Web_Dumper::Web_Dumper(IP4, std::string, std::string, std::string, std::string, std::string, long int)
     Web_Dumper(IP4  szip,const std::string sAgent,const std::string sVersion,const std::string sHost,const std::string sPath,const std::string sUlke,long int sTarih) : 
     ^
AnomalyDetector.c:1309:5: note:   no known conversion for argument 1 from ‘std::string {aka std::basic_string<char>}’ to ‘IP4’
AnomalyDetector.c:1300:8: note: Web_Dumper::Web_Dumper(const Web_Dumper&)
 struct Web_Dumper
        ^
AnomalyDetector.c:1300:8: note:   candidate expects 1 argument, 7 provided
AnomalyDetector.c:1300:8: note: Web_Dumper::Web_Dumper(Web_Dumper&&)
AnomalyDetector.c:1300:8: note:   candidate expects 1 argument, 7 provided
AnomalyDetector.c:1363:53: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘std::map<Web_Dumper, int>::size_type {aka long unsigned int}’ [-Wformat=]
     printf("Liste Boyutu : %d \n",http_dumper.size());
                                                     ^
AnomalyDetector.c:1366:217: warning: format ‘%d’ expects argument of type ‘int’, but argument 4 has type ‘long int’ [-Wformat=]
   printf("(%d). Ulke : %s | Tarih : %d | IP : %s | UA : %s | Host : %s | Path : %s \n",element.second,info.Ulke.c_str(),info.Tarih,decimal_to_ip(info.ip).c_str(),info.Agent.c_str(),info.Host.c_str(),info.Path.c_str());
                                                                                                                                                                                                                         ^
AnomalyDetector.c: In function ‘void Botnet_Ring(const pfring_pkthdr*, const u_char*, const u_char*)’:
AnomalyDetector.c:1378:8: warning: unused variable ‘dot_src_ip’ [-Wunused-variable]
  char *dot_src_ip = inet_ntoa(dst_addr);
        ^
AnomalyDetector.c: In function ‘int main(int, char**)’:
AnomalyDetector.c:1432:30: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
    if(device == NULL) device = "p1p1";
                              ^
AnomalyDetector.c:1440:87: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
     pfring_set_bpf_filter(ring_botnet, "tcp[((tcp[12:1] & 0xf0) >> 2):4] = 0x47455420");
                                                                                       ^
AnomalyDetector.c:1430:25: warning: unused variable ‘c’ [-Wunused-variable]
    char *device = NULL, c;
                         ^
AnomalyDetector.c:1457:55: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
     pfring_set_bpf_filter(ring, "tcp[13] == 2 or udp ");
                                                       ^
AnomalyDetector.c:1447:25: warning: unused variable ‘c’ [-Wunused-variable]
    char *device = NULL, c;
                         ^
make: *** [AnomalyDetector.o] Error 1

Open in new window

Avatar of ozo
ozo
Flag of United States of America image

You don't have a compare operator for your key values
Avatar of FireBall
FireBall

ASKER

Actually what i want to do is insert a struct into a map with this values at all, but i could not succeded
Hi Cahit Eyigunlu,

first thing IMO is you try to use std::pair and std::make_pair with more than two arguments allthoug std::pair is implemented to hold exactly two values. To use an arbitrary number of arguments/values you should use std::tuple and std::make_tuple (caution: this is part of C++ 14, if you don't have this you'll have to find any other solution).

ZOPPO
I just want to store this 5 values all together in an organizable array and a fast way for lookup inside
Yes, I understood, that's what you could do using tuples, with pair it's not possible.

Do you have a compiler which supports C++ 14 and tuples? If so it's fine, you can simply use it similar as you try with pair currently. If not you can try to find an alternative, i.e. Boost's tuple library boost::tuples.
SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you ozo ,

Do you have a very simple example for this ?
Just for interest: Above you wrote "... a fast way for lookup inside" - what exactly do you mean with this? If you want to have ability for fast searching by single values (so i.e. search all entries where "Host='xyz'"), the complete approach ist wrong, the way you do it above builds one key for the complete set of variables, so you even can only query for the same combination of variables.

If you want to be able to query per member you IMO need either one multimap per member (where the member is the key) or something else which is intended for such purpose like i.e.: boost::multi_index (see http://www.boost.org/doc/libs/1_53_0/libs/multi_index/doc/index.html).

ZOPPO
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The code posted by the Author had a compare operator but it didn't compile.

Ozo recommended to using std::unordered_map what is a valid solution (endorsed by the Author) if the not compiling operator< function was removed.

Zoppo recommended to using std::tuples instead of std::pair (the usage of pair was the reason for the Compiler errors)). This is a valid solution as std::tuples would provide a valid less operator for the combination of all tuple elements.

My solution was to implement a valid compare operator from scratch and I posted the complete and tested code.

Sara