using templates and map

S

Stoian

hey how is it goin,
i've got this problem - declared a template but it doesn't work
properly, here is the code:

53 std::map<Config, Config> seen;
54 Config new();
55 seen.insert(new, new);

i need to do it that way but the code brakes when i write the command
"seen.insert(new, new);"

i can assure you that everything else is working, just this - > i've
declared the template properly, cause i've used it in other parts of
the code, and i've also included the <map> library. by the way this is
the error, that i get - i also can't understand the error:

bash-2.05$ make
g++ -ggdb -c clock.cpp
/usr/local/gnu/lib/gcc/sparc-sun-solaris2.9/3.4.1/../../../../include/c++/3.4.1/bits/stl_tree.h:
In member function `void std::_Rb_tree<_Key, _Val, _KeyOfValue,
_Compare, _Alloc>::insert_unique(_II, _II) [with _InputIterator =
ClockConfig (*)(), _Key = ClockConfig, _Val = std::pair<const
ClockConfig, ClockConfig>, _KeyOfValue =
std::_Select1st<std::pair<const ClockConfig, ClockConfig> >, _Compare
= std::less<ClockConfig>, _Alloc = std::allocator<std::pair<const
ClockConfig, ClockConfig> >]':
/usr/local/gnu/lib/gcc/sparc-sun-solaris2.9/3.4.1/../../../../include/c++/3.4.1/bits/stl_map.h:397:
instantiated from `void std::map<_Key, _Tp, _Compare,
_Alloc>::insert(_InputIterator, _InputIterator) [with _InputIterator =
ClockConfig (*)(), _Key = ClockConfig, _Tp = ClockConfig, _Compare =
std::less<ClockConfig>, _Alloc = std::allocator<std::pair<const
ClockConfig, ClockConfig> >]'
Solver.cpp:55: instantiated from `void Solver<Config>::solve() [with
Config = ClockConfig]'
clock.cpp:60: instantiated from here
/usr/local/gnu/lib/gcc/sparc-sun-solaris2.9/3.4.1/../../../../include/c++/3.4.1/bits/stl_tree.h:993:
error: ISO C++ forbids incrementing a pointer of type `ClockConfig
(*)()'
/usr/local/gnu/lib/gcc/sparc-sun-solaris2.9/3.4.1/../../../../include/c++/3.4.1/bits/stl_tree.h:994:
error: no matching function for call to `std::_Rb_tree<ClockConfig,
std::pair<const ClockConfig, ClockConfig>,
std::_Select1st<std::pair<const ClockConfig, ClockConfig> >,
std::less<ClockConfig>, std::allocator<std::pair<const ClockConfig,
ClockConfig> > >::insert_unique(ClockConfig (&)())'
/usr/local/gnu/lib/gcc/sparc-sun-solaris2.9/3.4.1/../../../../include/c++/3.4.1/bits/stl_tree.h:862:
note: candidates are: std::pair<typename std::_Rb_tree<_Key, _Val,
_KeyOfValue, _Compare, _Alloc>::iterator, bool> std::_Rb_tree<_Key,
_Val, _KeyOfValue, _Compare, _Alloc>::insert_unique(const _Val&) [with
_Key = ClockConfig, _Val = std::pair<const ClockConfig, ClockConfig>,
_KeyOfValue = std::_Select1st<std::pair<const ClockConfig,
ClockConfig> >, _Compare = std::less<ClockConfig>, _Alloc =
std::allocator<std::pair<const ClockConfig, ClockConfig> >]
/usr/local/gnu/lib/gcc/sparc-sun-solaris2.9/3.4.1/../../../../include/c++/3.4.1/bits/stl_tree.h:888:
note: typename std::_Rb_tree<_Key, _Val, _KeyOfValue,
_Compare, _Alloc>::iterator std::_Rb_tree<_Key, _Val, _KeyOfValue,
_Compare, _Alloc>::insert_unique(std::_Rb_tree_iterator<_Val>, const
_Val&) [with _Key = ClockConfig, _Val = std::pair<const ClockConfig,
ClockConfig>, _KeyOfValue = std::_Select1st<std::pair<const
ClockConfig, ClockConfig> >, _Compare = std::less<ClockConfig>, _Alloc
= std::allocator<std::pair<const ClockConfig, ClockConfig> >]
*** Error code 1
make: Fatal error: Command failed for target `clock.o'


any help will be really appreciated!!!!
thanks a lot!!!
 
G

Gianni Mariani

Stoian said:
hey how is it goin,
i've got this problem - declared a template but it doesn't work
properly, here is the code:

53 std::map<Config, Config> seen;
54 Config new();

This defines a function called new() that returns a Config (prolly not
what you want).
55 seen.insert(new, new);

Inserting a function ?
i need to do it that way but the code brakes when i write the command
"seen.insert(new, new);"

i can assure you that everything else is working, just this - > i've
declared the template properly, cause i've used it in other parts of
the code, and i've also included the <map> library. by the way this is
the error, that i get - i also can't understand the error:


error snipped ... basically is says.

You're trying to call ...
....::insert_unique(ClockConfig (&)())

But a :
....::insert_unique(const _Val&)

is the only candidate.
 
J

John Harrison

Stoian said:
hey how is it goin,
i've got this problem - declared a template but it doesn't work
properly, here is the code:

53 std::map<Config, Config> seen;
54 Config new();
55 seen.insert(new, new);


1) new is not a legal name for a variable.
2) don't put backets after the variable name, that makes it a function.
3) you have to make a pair to insert into a map

std::map<Config, Config> seen;
Config noo;
seen.insert(std::make_pair(noo, noo));

That should compile.

john
 
S

Stoian

thanks a lot, i actually didn't notice when i writing these 3 lines
here, so i just put "new" and didn't realise (i didn't actually paste
the code). Ok i tried the make pair, and i still have errors, I'm not
that good with using templates, but i think that the problem is with
defining a comparator, probably the default one doesn't work with
templates, i don't know.
Here is what i did, and the error again:

50 Config temp;
51 std::map<Config, Config> seen;
52 seen.insert(std::make_pair(temp, temp));


bash-2.05$ make
g++ -ggdb -c change.cpp
/usr/local/gnu/lib/gcc/sparc-sun-solaris2.9/3.4.1/../../../../include/c++/3.4.1/
bits/stl_function.h: In member function `bool
std::less<_Tp>::eek:perator()(const _
Tp&, const _Tp&) const [with _Tp = ChangeConfig]':
/usr/local/gnu/lib/gcc/sparc-sun-solaris2.9/3.4.1/../../../../include/c++/3.4.1/
bits/stl_tree.h:869: instantiated from `std::pair<typename
std::_Rb_tree<_Key,
_Val, _KeyOfValue, _Compare, _Alloc>::iterator, bool>
std::_Rb_tree<_Key, _Val,
_KeyOfValue, _Compare, _Alloc>::insert_unique(const _Val&) [with _Key
= ChangeC
onfig, _Val = std::pair<const ChangeConfig, ChangeConfig>, _KeyOfValue
= std::_S
elect1st<std::pair<const ChangeConfig, ChangeConfig> >, _Compare =
std::less<Cha
ngeConfig>, _Alloc = std::allocator<std::pair<const ChangeConfig,
ChangeConfig>
/usr/local/gnu/lib/gcc/sparc-sun-solaris2.9/3.4.1/../../../../include/c++/3.4.1/
bits/stl_map.h:360: instantiated from `std::pair<typename
std::_Rb_tree<_Key,
re, _Alloc>::iterator, bool> std::map<_Key, _Tp, _Compare,
_Alloc>::insert(const
std::pair<const _Key, _Tp>&) [with _Key = ChangeConfig, _Tp =
ChangeConfig, _Co
mpare = std::less<ChangeConfig>, _Alloc =
std::allocator<std::pair<const ChangeC
onfig, ChangeConfig> >]'
Solver.cpp:52: instantiated from `void Solver<Config>::solve() [with
Config =
ChangeConfig]'
change.cpp:64: instantiated from here
/usr/local/gnu/lib/gcc/sparc-sun-solaris2.9/3.4.1/../../../../include/c++/3.4.1/
bits/stl_function.h:227: error: no match for 'operator<' in '__x <
__y'
*** Error code 1
make: Fatal error: Command failed for target `change.o'
bash-2.05$


thanks again for your help
 
V

Victor Bazarov

Stoian said:
thanks a lot, i actually didn't notice when i writing these 3 lines
here, so i just put "new" and didn't realise (i didn't actually paste
the code). Ok i tried the make pair, and i still have errors, I'm not
that good with using templates, but i think that the problem is with
defining a comparator, probably the default one doesn't work with
templates, i don't know.
Here is what i did, and the error again:

50 Config temp;
51 std::map<Config, Config> seen;
52 seen.insert(std::make_pair(temp, temp));

[...]
bits/stl_function.h:227: error: no match for 'operator<' in '__x <
__y'
*** Error code 1

Does your 'Config' class have operator< defined for it or in it? If not,
you cannot use it as the key in a map unless you define your own special
comparison functor. If you know how to compare two 'Config' objects,
then defining operator< is probably easier than anything else.

Victor
 
Z

Zeng Dinghao

You should define 'new' like this:

Config new;

rather than

Config new();

where the latter means 'define a funtion named new with void parameter list
and return Config'.

Stoian said:
hey how is it goin,
i've got this problem - declared a template but it doesn't work
properly, here is the code:

53 std::map<Config, Config> seen;
54 Config new();
55 seen.insert(new, new);

i need to do it that way but the code brakes when i write the command
"seen.insert(new, new);"

i can assure you that everything else is working, just this - > i've
declared the template properly, cause i've used it in other parts of
the code, and i've also included the <map> library. by the way this is
the error, that i get - i also can't understand the error:

bash-2.05$ make
g++ -ggdb -c clock.cpp
/usr/local/gnu/lib/gcc/sparc-sun-solaris2.9/3.4.1/../../../../include/c++/3.4.1/bits/stl_tree.h:
In member function `void std::_Rb_tree<_Key, _Val, _KeyOfValue,
_Compare, _Alloc>::insert_unique(_II, _II) [with _InputIterator =
ClockConfig (*)(), _Key = ClockConfig, _Val = std::pair<const
ClockConfig, ClockConfig>, _KeyOfValue =
std::_Select1st<std::pair<const ClockConfig, ClockConfig> >, _Compare
= std::less<ClockConfig>, _Alloc = std::allocator<std::pair<const
ClockConfig, ClockConfig> >]':
/usr/local/gnu/lib/gcc/sparc-sun-solaris2.9/3.4.1/../../../../include/c++/3.4.1/bits/stl_map.h:397:
instantiated from `void std::map<_Key, _Tp, _Compare,
_Alloc>::insert(_InputIterator, _InputIterator) [with _InputIterator =
ClockConfig (*)(), _Key = ClockConfig, _Tp = ClockConfig, _Compare =
std::less<ClockConfig>, _Alloc = std::allocator<std::pair<const
ClockConfig, ClockConfig> >]'
Solver.cpp:55: instantiated from `void Solver<Config>::solve() [with
Config = ClockConfig]'
clock.cpp:60: instantiated from here
/usr/local/gnu/lib/gcc/sparc-sun-solaris2.9/3.4.1/../../../../include/c++/3.4.1/bits/stl_tree.h:993:
error: ISO C++ forbids incrementing a pointer of type `ClockConfig
(*)()'
/usr/local/gnu/lib/gcc/sparc-sun-solaris2.9/3.4.1/../../../../include/c++/3.4.1/bits/stl_tree.h:994:
error: no matching function for call to `std::_Rb_tree<ClockConfig,
std::pair<const ClockConfig, ClockConfig>,
std::_Select1st<std::pair<const ClockConfig, ClockConfig> >,
std::less<ClockConfig>, std::allocator<std::pair<const ClockConfig,
ClockConfig> > >::insert_unique(ClockConfig (&)())'
/usr/local/gnu/lib/gcc/sparc-sun-solaris2.9/3.4.1/../../../../include/c++/3.4.1/bits/stl_tree.h:862:
note: candidates are: std::pair<typename std::_Rb_tree<_Key, _Val,
_KeyOfValue, _Compare, _Alloc>::iterator, bool> std::_Rb_tree<_Key,
_Val, _KeyOfValue, _Compare, _Alloc>::insert_unique(const _Val&) [with
_Key = ClockConfig, _Val = std::pair<const ClockConfig, ClockConfig>,
_KeyOfValue = std::_Select1st<std::pair<const ClockConfig,
ClockConfig> >, _Compare = std::less<ClockConfig>, _Alloc =
std::allocator<std::pair<const ClockConfig, ClockConfig> >]
/usr/local/gnu/lib/gcc/sparc-sun-solaris2.9/3.4.1/../../../../include/c++/3.4.1/bits/stl_tree.h:888:
note: typename std::_Rb_tree<_Key, _Val, _KeyOfValue,
_Compare, _Alloc>::iterator std::_Rb_tree<_Key, _Val, _KeyOfValue,
_Compare, _Alloc>::insert_unique(std::_Rb_tree_iterator<_Val>, const
_Val&) [with _Key = ClockConfig, _Val = std::pair<const ClockConfig,
ClockConfig>, _KeyOfValue = std::_Select1st<std::pair<const
ClockConfig, ClockConfig> >, _Compare = std::less<ClockConfig>, _Alloc
= std::allocator<std::pair<const ClockConfig, ClockConfig> >]
*** Error code 1
make: Fatal error: Command failed for target `clock.o'


any help will be really appreciated!!!!
thanks a lot!!!
 
R

Rolf Magnus

Stoian said:
thanks a lot, i actually didn't notice when i writing these 3 lines
here, so i just put "new" and didn't realise (i didn't actually paste
the code). Ok i tried the make pair, and i still have errors, I'm not
that good with using templates, but i think that the problem is with
defining a comparator, probably the default one doesn't work with
templates, i don't know.

The default one uses operator<, so your key type needs an operator<.
Here is what i did, and the error again:

50 Config temp;
51 std::map<Config, Config> seen;
52 seen.insert(std::make_pair(temp, temp));


bash-2.05$ make
g++ -ggdb -c change.cpp
....

/usr/local/gnu/lib/gcc/sparc-sun-solaris2.9/3.4.1/../../../../include/c++/3.4.1/
bits/stl_function.h:227: error: no match for 'operator<' in '__x <
__y'
*** Error code 1

The compiler is telling you that your key type (i.e. Config) doesn't have an
operator<. So you either need to use your own custom comparison
function/object or you need to overload operator< that compares two
instances of your Config class.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top