Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 62
  • Last Modified:

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

0
FireBall
Asked:
FireBall
  • 4
  • 3
  • 2
  • +1
3 Solutions
 
ozoCommented:
You don't have a compare operator for your key values
0
 
FireBallAuthor Commented:
Actually what i want to do is insert a struct into a map with this values at all, but i could not succeded
0
 
ZoppoCommented:
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
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
FireBallAuthor Commented:
I just want to store this 5 values all together in an organizable array and a fast way for lookup inside
0
 
ZoppoCommented:
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.
0
 
ozoCommented:
Keys for std::map need to have a compare operator.
If you don't want to define a compare operator for your struct, you might want to use std::unordered_map
1
 
ZoppoCommented:
That's right, but tuples have a operator < so it should work using them as keys for a map too ...
0
 
FireBallAuthor Commented:
Thank you ozo ,

Do you have a very simple example for this ?
0
 
ZoppoCommented:
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
0
 
sarabandeCommented:
I want to use given struct in a map as this : std::map<Web_Dumper, int> http_dumper;
if you look at the error message it is because you were using make_pair in the operator< but actually your map key is the struct Web_Dumper and the data of the map is int. so there isn't really a std::pair involved (nor a std::tuple). the only thing to do is to make Web_Dumper a valid key type for the map what only requires a proper operator< .

note, an operator== is not necessarily needed for a map key, since a == b can be implemented as  not a  < b and not b < a

#include <map>
#include <string>

unsigned int makeip4(const std::string & strip4)
{
    unsigned int ip4 = 0;
    size_t lpos = 0, pos;
    std::string s = strip4 + '.';
    int n = 0;
    while (n < 4 && (pos = s.find('.', lpos)) != std::string::npos)
    {
        ip4 *= 256;
        ip4 += atoi(s.substr(lpos, pos-lpos).c_str());
        n++;
        lpos = pos+1;
    }
    return ip4;
}

struct IP4
{
    unsigned int ip;
    unsigned short port;
    std::string sender;

    IP4(const std::string & strip4, unsigned short p, const std::string & s)
        : port(p), sender(s) { makeip4(strip4); } 
    friend bool operator<(const IP4 & ip1, const IP4 & ip2)
    {
        return (ip1.ip < ip2.ip) || (ip1.ip == ip2.ip && ip1.port < ip2.port);
    }
    friend bool operator==(const IP4 & ip1, const IP4 & ip2)
    {
        return (!(ip1 < ip2)) && (!(ip2 < ip1));
    }
};

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 (ipt1.ip <  ipt2.ip) ||
               (ipt1.ip == ipt2.ip && ipt1.Agent <  ipt2.Agent) ||
               (ipt1.ip == ipt2.ip && ipt1.Agent == ipt2.Agent && ipt1.Version <  ipt2.Version) ||
               (ipt1.ip == ipt2.ip && ipt1.Agent == ipt2.Agent && ipt1.Version == ipt2.Version && ipt1.Host <  ipt2.Host) ||
               (ipt1.ip == ipt2.ip && ipt1.Agent == ipt2.Agent && ipt1.Version == ipt2.Version && ipt1.Host == ipt2.Host && ipt1.Path <  ipt2.Path) ||
               (ipt1.ip == ipt2.ip && ipt1.Agent == ipt2.Agent && ipt1.Version == ipt2.Version && ipt1.Host == ipt2.Host && ipt1.Path == ipt2.Path && ipt1.Ulke <  ipt2.Ulke) ||
               (ipt1.ip == ipt2.ip && ipt1.Agent == ipt2.Agent && ipt1.Version == ipt2.Version && ipt1.Host == ipt2.Host && ipt1.Path == ipt2.Path && ipt1.Ulke == ipt2.Ulke && ipt1.Tarih < ipt2.Tarih);
    }
};

int main()
{
    std::map<Web_Dumper, int> webmap;
    IP4 ip4("10.1.2.254", 22222, "sender");
    Web_Dumper webd1(ip4, "agent", "version", "host", "path", "ulke", 333);
    Web_Dumper webd2(ip4, "agent", "version", "host", "path", "ulke", 444);
    webmap[webd1] = 12345;
    webmap[webd2] = 98765;
    return 0;
}

Open in new window


Sara
0
 
sarabandeCommented:
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
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now