943,846 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3272
  • C++ RSS
Apr 25th, 2009
0

Templated function fails: no matching function for call to

Expand Post »
Hey guys,

What am I doing wrong here?

sss.hpp
cpp Syntax (Toggle Plain Text)
  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
cpp Syntax (Toggle Plain Text)
  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:

C++ Syntax (Toggle Plain Text)
  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,
Reputation Points: 69
Solved Threads: 28
Posting Whiz
Clockowl is offline Offline
376 posts
since May 2008
Apr 25th, 2009
0

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

Okay dat was stoopid.

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

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

No wonder.
Reputation Points: 69
Solved Threads: 28
Posting Whiz
Clockowl is offline Offline
376 posts
since May 2008
Apr 27th, 2009
0

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

Hehe, back again. Templates are difficult if you're not precise I guess. I'm stuck on this one:

sss.hpp
cpp Syntax (Toggle Plain Text)
  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
cpp Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 69
Solved Threads: 28
Posting Whiz
Clockowl is offline Offline
376 posts
since May 2008
Apr 27th, 2009
0

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

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:
cpp Syntax (Toggle Plain Text)
  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. //...
Reputation Points: 69
Solved Threads: 28
Posting Whiz
Clockowl is offline Offline
376 posts
since May 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Gantor calculator
Next Thread in C++ Forum Timeline: dereferencing operators





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC