Templated function fails: no matching function for call to

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2008
Posts: 376
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

Templated function fails: no matching function for call to

 
0
  #1
Apr 25th, 2009
Hey guys,

What am I doing wrong here?

sss.hpp
  1. #include <fstream>
  2. #include <string>
  3. #include <map>
  4.  
  5. namespace sss {
  6. using namespace std;
  7. class node {
  8. public:
  9. multimap<string, string> values;
  10. multimap<string, node> children;
  11.  
  12. string &value(const string name, size_t index = 0);
  13. node &child(const string name, size_t index = 0);
  14.  
  15. private:
  16.  
  17. template <typename T1, typename T2>
  18. T2 &searchInsert(multimap<T1, T2> &container, const T1 &name, size_t index = 0) {
  19. typename multimap<T1, T2>::iterator it = container.find(name);
  20.  
  21. //found none, insert...
  22. if (it == container.end()) {
  23. it = container.insert(pair<T1, T2>(name, T2()));
  24. return it->second;
  25. }
  26.  
  27. //found one, multimap is sorted, iterate to index-th element or the end.
  28. for (size_t i = 0; i < index; i++) {
  29. it++;
  30. if (it == container.end()) {
  31. it = container.insert(pair<T1, T2>(name, T2()));
  32. return it->second;
  33. }
  34. }
  35.  
  36. return it->second;
  37. }
  38. };
  39. }

sss.cpp
  1. #include <iostream>
  2. #include <fstream>
  3. #include <map>
  4. #include <string>
  5.  
  6. #include "sss.hpp"
  7.  
  8. using namespace std;
  9. using sss::node;
  10.  
  11. string& node::value(const string name, size_t index) {
  12. return searchInsert<string, string>(values, name, index);
  13. }
  14.  
  15. node& node::child(const string name, size_t index) {
  16. return searchInsert<string, node>(values, name, index);
  17. }

It won't compile with the following errors:

  1. \Code\SSSParser\sss.cpp error: no matching function for call to `sss::node::searchInsert(std::multimap<std::string, std::string, std::less<std::string>, std::allocator<std::pair<const std::string, std::string> > >&, const std::string&, size_t&)'
  2.  

I don't really get what it's trying to do.

Thanks in advance,
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 376
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

Re: Templated function fails: no matching function for call to

 
0
  #2
Apr 25th, 2009
Okay dat was stoopid.

  1. node& node::child(const string name, size_t index) {
  2. return searchInsert<string, node>(values, name, index);
  3. }

Should of course be..
  1. node& node::child(const string name, size_t index) {
  2. return searchInsert<string, node>(children, name, index);
  3. }

No wonder.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 376
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

Re: Templated function fails: no matching function for call to

 
0
  #3
Apr 27th, 2009
Hehe, back again. Templates are difficult if you're not precise I guess. I'm stuck on this one:

sss.hpp
  1. #include <string>
  2. #include <map>
  3. #include <stdexcept>
  4.  
  5. #ifndef SSSHPP
  6. #define SSSHPP
  7.  
  8. namespace sss {
  9. using namespace std;
  10.  
  11. class node {
  12.  
  13. public:
  14. multimap<string, string> values;
  15. multimap<string, node> children;
  16.  
  17. private:
  18. template <typename T1, typename T2>
  19. T2 &get_indexed(multimap<T1, T2> &container, T1 &key, size_t index = 0) {
  20. size_t count = container.count(key);
  21.  
  22. if (count != 0 && count-1 >= index) {
  23. //it exists and index is within range
  24. //grab iterator, increment index times and return the node belonging to that.
  25. typename multimap<T1, T2>::iterator it = container.find(key);
  26. while (index--) it++;
  27. return it->second;
  28. } else {
  29. //Doesn't exist: throw OOR
  30. throw out_of_range((string)"get_indexed->"+"Element " + key + "doesn't exist!");
  31. }
  32. }
  33. };
  34. }
  35.  
  36. #endif // SSSHPP

Call with:

sss.cpp
  1. string &node::getValue(const string &name, size_t index) throw (out_of_range) {
  2. get_indexed<string, string>(values, name, index);
  3. }

Doesn't compile with:
  1. Code\SSSParser\sss.cpp In member function `std::string& sss::node::getValue(const std::string&, size_t)':
  2. Code\SSSParser\sss.cpp error: no matching function for call to `sss::node::get_indexed(std::multimap<std::string, std::string, std::less<std::string>, std::allocator<std::pair<const std::string, std::string> > >&, const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, size_t&)
  3.  
Last edited by Clockowl; Apr 27th, 2009 at 9:52 am.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 376
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

Re: Templated function fails: no matching function for call to

 
0
  #4
Apr 27th, 2009
Ugh, fixed it, I forgot the const. With normal functions the error messages are a lot clearer in GCC.. hope they "fix" this soon.

Here's the working code:
  1. //...
  2. private:
  3. template <typename T1, typename T2>
  4. T2 &get_indexed(multimap<T1, T2> &container, const T1 &key, size_t index = 0) {
  5. size_t count = container.count(key);
  6. //...
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC