I have recently started trying to teach myself C++ and am new to the board. I have created a strut called permuted_index, and a vector of permuted_index objects called perm_index.
I have written an overloaded predicate called "stringCompare" to be used with the sort function to sort either a vector of strings or a vector of permuted_index objects. However, when I try and run my program get error C2914 - which as I understand it means that the sort function can not identify which of version of stringCompare to use.
I have been looking at this for days and am completely stumped! I can force my program to work by commenting out one version of the predicate, but I would really like to understand the underlying program and would appreciate any help. I have provided everything that I think will help anyone looking at this below but if you need any more information please let me know.
This is the permuted index strut;
struct permuted_index{
std::string word ;
std::vector<std::string>::size_type line ;
std::string::size_type position ;
std::vector<std::string> full_line ;
std::vector<std::string> rotated_line ;
std::string before_word ;
std::string after_word ; };
This is the overloaded predicate;
#include <string>
#include <vector>
#include "split.h"
#include "Permuted_index.h"
using std::string ;
using std::vector ;
bool stringCompare(const string& x, const string& y) {
vector<string> p ;
vector<string> q ;
p = split(x) ;
q = split(y) ;
return p[0] < q[0] ;
}
bool stringCompare(const permuted_index& x, const permuted_index& y){
return x.rotated_line[0] < y.rotated_line[0] ;
}
This is the split function called above;
vector<string> split(const string& s)
{
vector<string> ret ;
typedef string::size_type string_size ;
string_size i = 0 ;
while(i != s.size())
{
while(i != s.size() && isspace(s[i]))
{
++i ;
}
string_size j = i ;
while(j != s.size() && !isspace(s[j]))
{
++j ;
}
if(i != j)
{
ret.push_back(s.substr(i, j-i)) ;
i = j ;
}
}
return ret ;
}
The line within my main() program that is causing the error is;
sort(perm_index.begin(), perm_index.end(), stringCompare) ;
and the the exact error message is:
error C2914: 'std::sort' : cannot deduce template argument as function argument is ambiguous