Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > std::map with user defined data

Reply
Thread Tools

std::map with user defined data

 
 
mergaite_lietuvaite@yahoo.de
Guest
Posts: n/a
 
      01-17-2006
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:air<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:air<const _Key, _Tp>,
std::_Select1st<std:air<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:air<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

 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      01-17-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.

> ....
> }


 
Reply With Quote
 
 
 
 
Earl Purple
Guest
Posts: n/a
 
      01-17-2006

(E-Mail Removed) 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: __STDC_IEC_559__ (defined or !defined ?) Keith Thompson C Programming 0 08-17-2010 04:36 PM
User-defined exception: "global name 'TestRunError' is not defined" jmike@alum.mit.edu Python 1 07-10-2008 12:37 PM
defined? for recursive function call v/s defined? for function call stack Alok Ruby 3 04-13-2006 11:53 AM
Using parenthesis with defined (#if defined(...)) Angel Tsankov C++ 1 04-05-2006 10:00 PM
#if (defined(__STDC__) && !defined(NO_PROTOTYPE)) || defined(__cplusplus) Oodini C Programming 1 09-27-2005 07:58 PM



Advertisments