Like Tree1Likes

bool operator< issues

This is a discussion on bool operator< issues within the C++ Programming forums, part of the General Programming Boards category; I'm currently working on a homework assignment where I have a vector of custom class objects. I want to sort ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    32

    bool operator< issues

    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.

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    The last one of your variations is essentially correct, but you're missing the fileData:: to attach it to the class!
    Code:
    bool inline fileData::operator<(const fileData& f) const
    {
        return strcmp(directName, f.directName) < 0;
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    32
    Quote Originally Posted by oogabooga View Post
    The last one of your variations is essentially correct, but you're missing the fileData:: to attach it to the class!
    Code:
    bool inline fileData::operator<(const fileData& f) const
    {
        return strcmp(directName, f.directName) < 0;
    }
    I gave it a shot, but it still gives one error, although shorter:
    Code:
    ./fileData.hpp:20:27: error: extra qualification on member 'operator<'
        bool inline fileData::operator<(const fileData& f) const { return...
                    ~~~~~~~~~~^

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    I thought you were defining it outside of the class. But if it's in the class then get rid of both "inline" and "fileData::". Members defined in the class are automatically inline and connected to that class.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    32
    No, I'm defining this as an inline in the header.

    I tried adjusting it again, and as I feared, I started getting unrelated error messages from the constructor.

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Here's a little example that works.
    If you still have problems, you may have to post more code.
    Code:
    // g++ -std=c++11 -Wall lessthan.cpp
    
    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <cstring>
    
    class A {
        static const size_t MAXSTR = 100;
        char cstr[MAXSTR];
    public:
        A(const char *cstr) {
            std::strncpy(this->cstr, cstr, MAXSTR);
            this->cstr[MAXSTR-1] = '\0';
        }
         bool operator<(const A &a) const {
            return std::strcmp(cstr, a.cstr) < 0;
        }
        friend std::ostream& operator<<(std::ostream& os, const A& a);
    };
    
    std::ostream& operator<<(std::ostream& os, const A& a) {
        return os << a.cstr;
    }
    
    int main() {
        std::vector<A> v = {"def", "abc", "jkl", "pqr", "mno", "ghi"};
        std::sort(v.begin(), v.end());
        for (auto &x: v)
            std::cout << x << '\n';
        return 0;
    }
    Last edited by oogabooga; 09-26-2013 at 09:03 PM.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  7. #7
    Registered User
    Join Date
    Sep 2012
    Posts
    32
    oogabooga, your code did not compile on my computer, either for c++ or g++. g++ gave me these errors in the terminal:
    Code:
    d.cpp: In function ‘int main()’:
    d.cpp:25: error: scalar object ‘v’ requires one element in initializer
    d.cpp:27: error: a function-definition is not allowed here before ‘:’ token
    d.cpp:29: error: expected primary-expression before ‘return’
    d.cpp:29: error: expected `;' before ‘return’
    d.cpp:29: error: expected primary-expression before ‘return’
    d.cpp:29: error: expected `)' before ‘return’

  8. #8
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    It's C++11, so with g++:
    g++ -std=c++11 -Wall lessthan.cpp

    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  9. #9
    Registered User
    Join Date
    Sep 2012
    Posts
    32
    It wouldn't take the -std argument. "Unrecognized."

  10. #10
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Here's an old-style C++ version.
    Code:
    // g++ -Wall lessthan.cpp
    
    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <cstring>
    
    class A {
        static const size_t MAXSTR = 100;
        char cstr[MAXSTR];
    public:
        A(const char *cstr) {
            std::strncpy(this->cstr, cstr, MAXSTR);
            this->cstr[MAXSTR-1] = '\0';
        }
        bool operator<(const A &a) const {
            return std::strcmp(cstr, a.cstr) < 0;
        }
        friend std::ostream& operator<<(std::ostream& os, const A& a);
    };
    
    std::ostream& operator<<(std::ostream& os, const A& a) {
        return os << a.cstr;
    }
    
    int main() {
        std::vector<A> v;
        v.push_back("def");
        v.push_back("abc");
        v.push_back("pqr");
        v.push_back("mno");
        std::sort(v.begin(), v.end());
        for (size_t i = 0; i < v.size(); i++)
            std::cout << v[i] << '\n';
        return 0;
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,302
    Then try: -std=c++0x as I believe c++11 is only recognised in g++ 4.7+
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by laserlight View Post
    Then try: -std=c++0x as I believe c++11 is only recognised in g++ 4.7+
    c++0x sounds like they were hoping it would come out before 2010.
    What is the pre-c++11 version? I don't know what to call it.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  13. #13
    Registered User
    Join Date
    Sep 2012
    Posts
    32
    Nope. Not working.

    (At this point I should mention I'm running OSX ML 10.8.5 and Xcode v.5.0 with command line tools extension.)

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,302
    Quote Originally Posted by oogabooga
    What is the pre-c++11 version? I don't know what to call it.
    C++03, though because it was mainly a bunch of minor corrections to C++98, you would still use -std=c++98 with g++.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by laserlight View Post
    C++03, though because it was mainly a bunch of minor corrections to C++98, you would still use -std=c++98 with g++.
    Okay, thanks. C++98 sounds familiar.

    BTW, you're going to have to help this guy. It's kind of the blind leading the blind at this point.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overload the..bool operator..?
    By 39ster in forum C++ Programming
    Replies: 18
    Last Post: 11-26-2008, 06:24 AM
  2. BOOL bool ? unresolved external symbol
    By xwielder in forum C Programming
    Replies: 6
    Last Post: 05-20-2008, 09:39 AM
  3. How get bool's from std::vector<bool>
    By 6tr6tr in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2008, 06:24 AM
  4. Script errors - bool unrecognized and struct issues
    By ulillillia in forum Windows Programming
    Replies: 10
    Last Post: 12-18-2006, 04:44 AM
  5. Interesting: bool != BOOL
    By lightatdawn in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 01-09-2002, 11:09 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21