Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6874307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:10:41+00:00 2026-05-27T04:10:41+00:00

I’m a beginner at C++ and have a question regarding sorting a vector of

  • 0

I’m a beginner at C++ and have a question regarding sorting a vector of a class object. I’ve done a lot of research online but can’t seem to figure out the answer. My problem is that I want to have multiple predicate comparison functions that I can name, as I need to have the user be able to choose from a menu which private member variable/component of the vector to sort on. I’m able to get a predicate comparison function with an overloaded operator to work ok, but when I try and name the function, I can’t seem to get it to compile without a bunch of errors (which I’ll post farther down underneath the appropriate code).

When the code’s run, a vector of a BookData object is created by a function that reads a text file. Function prototype in main:

vector<BookData> createVector();

Function call in main:

vector<BookData> books = createVector();

createVector function definition:

vector<BookData> createVector()
{
    const int LENGTH = 81;
    char input[LENGTH];
    vector<BookData> fBooks;
    string vBookTitle, vIsbn, vAuthor, vPublisher, vDateAdded;
    int vQtyOnHand;
    double vWholesale, vRetail;
    BookData *books1;
    books1 = new BookData;
    fstream dataFile("Serendipity.data");
    if (dataFile.is_open())
    {
        while (!dataFile.eof())
        {
            dataFile.getline(input, LENGTH, '\t');
            vBookTitle = input;
            books1->setTitle(vBookTitle);
            dataFile.getline(input, LENGTH, '\t');
            vAuthor = input;
            books1->setAuthor(vAuthor);
            dataFile.getline(input, LENGTH, '\t');
            vPublisher = input;
            books1->setPub(vPublisher);
            dataFile.getline(input, LENGTH, '\t');
            vIsbn = input;
            books1->setIsbn(vIsbn);
            dataFile >> vQtyOnHand;
            books1->setQty(vQtyOnHand);
            dataFile >> vWholesale;
            books1->setWholesale(vWholesale);
            dataFile >> vRetail;
            books1->setRetail(vRetail);
            dataFile.ignore();
            dataFile.getline(input, LENGTH);
            vDateAdded = input;
            books1->setDateAdded(vDateAdded);
            fBooks.push_back(*books1);
        }
    }
    return fBooks;
}

This is my class definition file (bookdata.h) with the working comparison function:

class BookData
{ 
private:
        string bookTitle, isbn, author,
            publisher, dateAdded;
        int qtyOnHand;
        double wholesale, retail;
        bool empty;     

public:
    BookData();
    bool operator< (BookData rhs);
    void printVector();
    void setTitle(string);
    void setIsbn(string);
    void setAuthor(string);
    void setPub(string);
    void setDateAdded(string);
    void setQty(int);
    void setWholesale(double);
    void setRetail(double);
    int isEmpty();
    void insertBook();
    void removeBook();
    string getTitle();
    string getIsbn();
    string getAuthor();
    string getPublisher();
    string getDateAdded();
    int getQtyOnHand();
    double getWholesale();
    double getRetail();
};

And this is the working function in the class implementation file (bookdata.cpp):

bool BookData::operator< (BookData rhs)
{
    return qtyOnHand < rhs.qtyOnHand;
}

And it’s called like so from int main():

sort (books.begin(), books.end());

However, if I try and name the function:

bool compare (BookData rhs);

from within the class definition file and change it to this in the class implementation file:

bool BookData::compare (BookData rhs)
{
    return qtyOnHand < rhs.qtyOnHand;
}

and change the vector sort function so within main:

sort (books.begin(), books.end(), &BookData::compare);

I get an error message from the compiler:

error C2064: term does not evaluate to a function taking 2 arguments

Any suggestions as to how I can properly name/call the sort function?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T04:10:42+00:00Added an answer on May 27, 2026 at 4:10 am

    The Compare shouldn’t be a member of the class (or be a static member or friend if it accesses restricted fields), and it should accept both the values to compare (whereas operator < uses this as the left value).

    something like this:

    bool compare (BookData lhs, BookData rhs)
    {
        return lhs.qtyOnHand < rhs.qtyOnHand;
    }
    

    Using operator< is IMHO a better approach.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a French site that I want to parse, but am running into
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
this is what i have right now Drawing an RSS feed into the php,
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.