By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,910 Members | 1,077 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,910 IT Pros & Developers. It's quick & easy.

using templates and map

P: n/a
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!!!
Jul 22 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Stoian wrote:
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.
Jul 22 '05 #2

P: n/a
> 53 std::map<Config, Config> seen;
54 Config foo;
55 seen.insert(std::make_pair(foo, foo));


new - key word
Config foo() - declaration of function
Jul 22 '05 #3

P: n/a

"Stoian" <sp*****@yahoo.com> wrote in message
news:4a**************************@posting.google.c om...
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
Jul 22 '05 #4

P: n/a
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>::operator()(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,
std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key, _Tp>, _Compa

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
Jul 22 '05 #5

P: n/a
"Stoian" <sp*****@yahoo.com> wrote...
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
Jul 22 '05 #6

P: n/a
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" <sp*****@yahoo.com>
??????:4a**************************@posting.google .com...
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!!!

Jul 22 '05 #7

P: n/a
Stoian wrote:
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.

Jul 22 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.