Maps and constructors and other crap

This is a discussion on Maps and constructors and other crap within the C++ Programming forums, part of the General Programming Boards category; Grettings and salutations :) I have the following two classes: Code: //------------------------------------------ // VHSTATS Class //------------------------------------------ class VHSTATS { public: ...

  1. #1
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986

    Maps and constructors and other crap

    Grettings and salutations :)

    I have the following two classes:
    Code:
    //------------------------------------------
    //  VHSTATS Class
    //------------------------------------------
    class VHSTATS
    {
    public:
        VHSTATS();
        VHSTATS(VIRTUALHOST);
        map <string, int> PageRequests;
        unsigned long NumberOfRequests;
        unsigned long BytesSent; 
        VIRTUALHOST ThisVH;  // VIRTUALHOST is a structure with info and strings and crap
    };
    
    //------------------------------------------
    //  STATS Class
    //------------------------------------------
    class STATS
    {
      public:
        STATS(); 
        bool WriteStatsFile();
        
        unsigned long NumberOfRequests;
        unsigned long TotalNumberOfRequests;
        unsigned long BytesSent;
        unsigned long TotalBytesSent;
        string LastRestart; 
        map <string, int> PageRequests;
        map <VIRTUALHOST, VHSTATS> VirtualHosts;basis 
    };
    STATS::STATS() looks something like this:
    Code:
    STATS::STATS()
    {
        // 1: Load all the virtual hosts
        int X = 0;
        while (VHI.HostNumbers[X].empty() == false)
        {
            string Name = VHI.HostNumbers[X];
            VHSTATS TempVHStats(VHI.Host[Name]); 
            VirtualHosts[VHI.Host[Name]] = TempVHStats;
        }
        .....
    Where VHI.Host is an map of strings to VIRTUALHOST's, and VHI.HostNumbers is a map of integers to strings (that match the strings used by VHI.Host).

    When I try to compile, VC++ gives me this:
    Code:
    stats.cpp
    C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE\functional(86) : error C2784: 'bool __cdecl std::operator <(const class std::multimap<_K,_Ty,_Pr,_A> &,const class std::multimap<_K,_Ty,_Pr,_A> &)' : could not deduce template argument for 'const
     class std::multimap<_K,_Ty,_Pr,_A> &' from 'const class VIRTUALHOST'
            C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE\functional(86) : while compiling class-template member function 'bool __thiscall std::less<class VIRTUALHOST>::operator ()(const class VIRTUALHOST &,const class VIRTUALHOST &) const'
    C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE\functional(86) : error C2784: 'bool __cdecl std::operator <(const class std::map<_K,_Ty,_Pr,_A> &,const class std::map<_K,_Ty,_Pr,_A> &)' : could not deduce template argument for 'const class std
    ::map<_K,_Ty,_Pr,_A> &' from 'const class VIRTUALHOST'
            C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE\functional(86) : while compiling class-template member function 'bool __thiscall std::less<class VIRTUALHOST>::operator ()(const class VIRTUALHOST &,const class VIRTUALHOST &) const'
    C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE\functional(86) : error C2784: 'bool __cdecl std::operator <(const class std::_Tree<_K,_Ty,_Kfn,_Pr,_A> &,const class std::_Tree<_K,_Ty,_Kfn,_Pr,_A> &)' : could not deduce template argument for 'c
    onst class std::_Tree<_K,_Ty,_Kfn,_Pr,_A> &' from 'const class VIRTUALHOST'
            C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE\functional(86) : while compiling class-template member function 'bool __thiscall std::less<class VIRTUALHOST>::operator ()(const class VIRTUALHOST &,const class VIRTUALHOST &) const'
    C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE\functional(86) : error C2784: 'bool __cdecl std::operator <(const class std::basic_string<_E,_Tr,_A> &,const _E *)' : could not deduce template argument for 'const class std::basic_string<_E,_Tr,
    _A> &' from 'const class VIRTUALHOST'
            C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE\functional(86) : while compiling class-template member function 'bool __thiscall std::less<class VIRTUALHOST>::operator ()(const class VIRTUALHOST &,const class VIRTUALHOST &) const'
    C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE\functional(86) : error C2784: 'bool __cdecl std::operator <(const _E *,const class std::basic_string<_E,_Tr,_A> &)' : could not deduce template argument for 'const  *' from 'const class VIRTUALHO
    ST'
            C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE\functional(86) : while compiling class-template member function 'bool __thiscall std::less<class VIRTUALHOST>::operator ()(const class VIRTUALHOST &,const class VIRTUALHOST &) const'
    C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE\functional(86) : error C2784: 'bool __cdecl std::operator <(const class std::basic_string<_E,_Tr,_A> &,const class std::basic_string<_E,_Tr,_A> &)' : could not deduce template argument for 'const
     class std::basic_string<_E,_Tr,_A> &' from 'const class VIRTUALHOST'
            C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE\functional(86) : while compiling class-template member function 'bool __thiscall std::less<class VIRTUALHOST>::operator ()(const class VIRTUALHOST &,const class VIRTUALHOST &) const'
    C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE\functional(86) : error C2784: 'bool __cdecl std::operator <(const class std::reverse_iterator<_RI,_Ty,_Rt,_Pt,_D> &,const class std::reverse_iterator<_RI,_Ty,_Rt,_Pt,_D> &)' : could not deduce te
    mplate argument for 'const class std::reverse_iterator<_RI,_Ty,_Rt,_Pt,_D> &' from 'const class VIRTUALHOST'
            C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE\functional(86) : while compiling class-template member function 'bool __thiscall std::less<class VIRTUALHOST>::operator ()(const class VIRTUALHOST &,const class VIRTUALHOST &) const'
    C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE\functional(86) : error C2784: 'bool __cdecl std::operator <(const struct std::pair<_T1,_T2> &,const struct std::pair<_T1,_T2> &)' : could not deduce template argument for 'const struct std::pair<
    _T1,_T2> &' from 'const class VIRTUALHOST'
            C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE\functional(86) : while compiling class-template member function 'bool __thiscall std::less<class VIRTUALHOST>::operator ()(const class VIRTUALHOST &,const class VIRTUALHOST &) const'
    C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE\functional(86) : error C2676: binary '<' : 'const class VIRTUALHOST' does not define this operator or a conversion to a type acceptable to the predefined operator
            C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE\functional(86) : while compiling class-template member function 'bool __thiscall std::less<class VIRTUALHOST>::operator ()(const class VIRTUALHOST &,const class VIRTUALHOST &) const'
    Error executing cl.exe.
    
    SWEBS.exe - 9 error(s), 0 warning(s)
    All of them seem to be in files which I have not touched. Can anyone see what it is I must have done wrong? I'm stumped on this one, it should work fine :( Thanks for any help!

  2. #2
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    When you are using user defind objects as elemets in your (STL)containers they must satify some requirements like

    • Element must implement a default constructor
    • Element must be copyable (implement copy constructor)
    • Element must be assignable (overload assignment operator =)
    • Element must be destoyable (implement destructor)
    • Element must be compareable (operator ==, operator <, operator > ...)


    Of course doesnīt you need to implement all these requirement (but this is recommended), because different container requires different requirements. E.i If you are using VIRTUALHOST objects in a stack there is no need for compareison but in a map there is.

    Make your VIRTUALHOST- and VHSTATSobjects compareable(I think that it should be enough with operator<) and your problem should be solved.

    P.S. Donīt rely on that the compiler will create itīs defaults version of constructor, destructor, operator==... in most cases this isnīt enough and the containers may not behave as it should.
    Last edited by ripper079; 09-06-2003 at 09:25 AM.
    01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
    Good things donīt come easy in life!!!

  3. #3
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Thanks ripper079, I did what you said and it compiled perfectly!! Thanks a lot!

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21