I'm currently working on a homework assignment where I have a vector of custom class objects. I want to sort these vector entries in alphabetical order. I've tried using my instructor's method of implementing the boolean operator for <, but it isn't working properly for me. Below is the header file involved with this issue:
Code:
class fileData{
private:
char directName[256], *directType;
int inodeNum;
//---------
int statNode, statMode, statLink, statUID, statGID, statMTime, statCTime;
//---------
char SHA1[20];
public:
fileData(direntry d, stats s);
~fileData();
bool operator<(fileData f) { return (strcmp(directName, f.directName) < 0);}
void print(ostream& out);
void serialize(ostream& out);
};
In the other class file, I've got:
Code:
class dirData{
private:
//...
vector<fileData> fD;
//...
public:
dirData(char* name, stats s);
~dirData();
//...
void sortFD(){ std::sort(fD.begin(), fD.end());}
//...
};
These are the errors I'm getting when trying to compile through the terminal:
Code:
/usr/include/c++/4.2.1/bits/stl_algo.h:91:15: error: invalid operands to binary
expression ('const fileData' and 'const fileData')
if (__a < __b)
~~~ ^ ~~~
/usr/include/c++/4.2.1/bits/stl_algo.h:2752:23: note: in instantiation of
function template specialization 'std::__median<fileData>' requested here
_ValueType(std::__median(*__first,
^
/usr/include/c++/4.2.1/bits/stl_algo.h:2829:4: note: in instantiation of
function template specialization
'std::__introsort_loop<__gnu_cxx::__normal_iterator<fileData *,
std::vector<fileData, std::allocator<fileData> > >, long>' requested here
std::__introsort_loop(__first, __last,
^
./dirData.hpp:24:20: note: in instantiation of function template specialization
'std::sort<__gnu_cxx::__normal_iterator<fileData *, std::vector<fileData,
std::allocator<fileData> > > >' requested here
void sortFD(){ std::sort(fD.begin(), fD.end());}
^
./fileData.hpp:20:10: note: candidate function not viable: 'this' argument has
type 'const fileData', but method is not marked const
bool operator<(fileData f) { return (strcmp(directName, f.directName) < 0);}
^
/usr/include/c++/4.2.1/bits/stl_pair.h:102:5: note: candidate template ignored:
failed template argument deduction
operator<(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
^
/usr/include/c++/4.2.1/bits/stl_iterator.h:288:5: note: candidate template
ignored: failed template argument deduction
operator<(const reverse_iterator<_Iterator>& __x,
^
/usr/include/c++/4.2.1/bits/stl_iterator.h:338:5: note: candidate template
ignored: failed template argument deduction
operator<(const reverse_iterator<_IteratorL>& __x,
^
/usr/include/c++/4.2.1/bits/stl_vector.h:959:5: note: candidate template
ignored: failed template argument deduction
operator<(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
^
/usr/include/c++/4.2.1/bits/basic_string.h:2225:5: note: candidate template
ignored: failed template argument deduction
operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
^
/usr/include/c++/4.2.1/bits/basic_string.h:2237:5: note: candidate template
ignored: failed template argument deduction
operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
^
/usr/include/c++/4.2.1/bits/basic_string.h:2249:5: note: candidate template
ignored: failed template argument deduction
operator<(const _CharT* __lhs,
Needless to say, I'm a little confused as to why it's not working, and would appreciate any explanation as to why.
A few things I should point out:
1) I have a header containing all the headers I need for the assignment. Especially included are <algorithm> and <sys/dir.h>. I've ruled out the headers in my issues, but may be wrong.
2) I've tried a few variations:
Code:
fileData::operator<(fileData f) { return (strcmp(directName, f.directName) < 0);}
inline operator<(fileData f) { return (strcmp(directName, f.directName) < 0);}
bool inline operator<(fileData f) { return (strcmp(directName, f.directName) < 0);}
bool inline operator<(fileData& f) { return (strcmp(directName, f.directName) < 0);}
bool inline operator<(const fileData& f) { return (strcmp(directName, f.directName) < 0);}
bool inline operator<(const fileData& f) const { return (strcmp(directName, f.directName) < 0);}
The first two need a function type, the middle three produce the same error, and the last one somehow manages to return "expected unqualified-id" error for when I'm initializing data objects in the constructor.