std::map with user defined data

Discussion in 'C++' started by mergaite_lietuvaite@yahoo.de, Jan 17, 2006.

  1. Guest

    Hi,

    I try to use map. My code is very easy, but there are some errors....
    (by compiling). Plaese, could you say, what I do wrong..

    struct less_array
    {
    bool operator()(const FArray p1, const FArray p2) const
    {
    return p1 < p2 ;
    }
    };

    int main(){

    ....

    map<const FArray, vector<FArray>, less_array> my_map;

    const vector<FArray>& vert1 = .....getVert1(); // a vector of.
    vector<FArray>::const_iterator vert = vert1.begin();

    if(my_map.find(vert) == my_map.end()) // line 172 in code
    cout<<"map is empty"<<endl;

    my_map[vert]= new FArray(); // line 194 in code

    .....
    }

    Compiler gets error-message because of my_map-find and my_map[vert].
    Compiler message is:
    ....
    FDualMarchingCubesAlgorithm.cc:176: error: no matching function for
    call to `
    std::map<const FArray, std::vector<FArray, std::allocator<FArray> >,
    FDualMarchingCubesAlgorithm::less_array,
    std::allocator<std::pair<const FArray,
    std::vector<FArray, std::allocator<FArray> > > > >::find(
    __gnu_cxx::__normal_iterator<const FArray*, std::vector<FArray,
    std::allocator<FArray> > >&)'
    /usr/include/g++/bits/stl_map.h:468: error: candidates are: typename
    std::_Rb_tree<_Key, std::pair<const _Key, _Tp>,
    std::_Select1st<std::pair<const _Key, _Tp> >, _Compare,
    _Alloc>::iterator
    std::map<_Key, _Tp, _Compare, _Alloc>::find(const _Key&) [with _Key
    = const
    FArray, _Tp = std::vector<FArray, std::allocator<FArray> >, _Compare
    =
    FDualMarchingCubesAlgorithm::eqpos, _Alloc =
    std::allocator<std::pair<const
    FArray, std::vector<FArray, std::allocator<FArray> > > >]
    .....

    DualMarchingCubesAlgorithm.cc:194: error: no match for 'operator[]' in
    '
    my_map[vert]'
    ......

    thank you on advance!

    Joana
    , Jan 17, 2006
    #1
    1. Advertising

  2. Rolf Magnus Guest

    wrote:

    > Hi,
    >
    > I try to use map. My code is very easy, but there are some errors....
    > (by compiling). Plaese, could you say, what I do wrong..
    >
    > struct less_array
    > {
    > bool operator()(const FArray p1, const FArray p2) const
    > {
    > return p1 < p2 ;
    > }
    > };


    Not an error, but you should probably pass the arguments by reference to
    ensure they are not unnecessarily copied. But then, you don't need the
    whole struct, since the above is the default behavior for maps.

    > int main(){
    >
    > ...
    >
    > map<const FArray, vector<FArray>, less_array> my_map;
    >
    > const vector<FArray>& vert1 = .....getVert1(); // a vector of.
    > vector<FArray>::const_iterator vert = vert1.begin();
    >
    > if(my_map.find(vert) == my_map.end()) // line 172 in code


    vert is a const_iterator. Your map's key type is not such an iterator type,
    so you can't use it as argument type for find(). You probably wanted:

    if(my_map.find(*vert) == my_map.end())

    > cout<<"map is empty"<<endl;
    >
    > my_map[vert]= new FArray(); // line 194 in code


    The value type of your map is not FArray*, but std::vector<FArray>, so the
    right side of your assignment needs to be of that type.

    > ....
    > }
    Rolf Magnus, Jan 17, 2006
    #2
    1. Advertising

  3. Earl Purple Guest

    wrote:
    >
    > map<const FArray, vector<FArray>, less_array> my_map;
    >
    > vector<FArray>::const_iterator vert = vert1.begin();
    >
    > my_map[vert]= new FArray(); // line 194 in code
    >
    > ....
    > }


    To answer just the question asked, it is invalid because vert is an
    iterator, not an FArray. Also the RHS will not convert from FArray * to
    vector< FArrray >

    my_map[ *vert ] = vector< FArray > ( 1 );

    will work albeit that I'm a bit questionable about your design.
    Earl Purple, Jan 17, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Jansson
    Replies:
    5
    Views:
    6,304
    Ivan Vecerina
    Mar 17, 2005
  2. Oodini
    Replies:
    1
    Views:
    1,768
    Keith Thompson
    Sep 27, 2005
  3. Replies:
    1
    Views:
    420
    red floyd
    Dec 21, 2008
  4. Thomas J. Gritzan
    Replies:
    6
    Views:
    1,020
    James Kanze
    Dec 22, 2008
  5. James Kanze
    Replies:
    0
    Views:
    1,999
    James Kanze
    Dec 21, 2008
Loading...

Share This Page