Solved

How to call a friend function from within another function's parameter list.

Posted on 2003-11-10
8
531 Views
Last Modified: 2008-03-06
Hello all:

I am having a problem with a compare_last_name function that I have defined as a friend of a Book class. I am trying to call this function from within a sort function in the main program. I can't seem to get the parameters correct for compare_last_name function within the sort function's parameter list.

thank you for any help

 ---------------------------------------

//file name: book8.h

#include <string>
using namespace std;

# ifndef BOOK_H
# define BOOK_H

class Book
{
      public:
            Book() { };
            Book(string an_author_first_name, string an_author_last_name,
                   string a_title,string a_publisher, string an_isbn,
                   string a_pub_year, string a_page_count, string a_price,
                   string book_type);
//            ~Book();
            
            string get_author_first_name() const;
                         .
                                             .
                                             .
                                             .                
                                   .
                                             .
                                             .
                                             .
            friend istream& operator>>(istream&, Book&);
            friend ostream& operator<<(ostream&, Book&);
            friend bool compare_last_name(const Book&, const Book&);
            
            void print_books();

      private:
            string author_first_name;
            string author_last_name;
            string title;
            string publisher;
            string isbn;
            string pub_year;
            string page_count;
            string price;
            string book_type;
};
#endif;

// file name: book8.cpp
// implementation file for Book class

#include <iostream>
#include <sstream>
#include <iomanip>
#include "book8.h"

using namespace std;

string Book::get_author_first_name() const
{
      return author_first_name;
}
                         .
                         .
                         .
                         .

istream& operator>>(istream& infile, Book& a_book)
{
 
      istringstream strin;
      string line;

      getline(infile, line);
      strin.str(line);
      strin  >> a_book.author_first_name >> a_book.author_last_name
               >> a_book.title >> a_book.publisher >> a_book.isbn
               >> a_book.pub_year >> a_book.page_count
               >> a_book.price >> a_book.book_type ;
 
      return infile;
}

                             
ostream& operator<<(ostream& outfile, Book& a_book)
{
 
      cout    << a_book.author_first_name << " " << a_book.author_last_name
                << " " << a_book.title << " " << a_book.publisher << " " 
                  << a_book.isbn << " " <<  a_book.pub_year << " "
                  << a_book.page_count << " " << a_book.price << " "
                  << a_book.book_type << endl  ;      
      
      outfile << a_book.author_first_name << " " << a_book.author_last_name
                << " " << a_book.title << " " << a_book.publisher << " " 
                  << a_book.isbn << " " <<  a_book.pub_year << " "
                  << a_book.page_count << " " << a_book.price << " "
                  << a_book.book_type << endl  ;      
      
      return outfile;
}

bool compare_last_name(const Book& b_book, const Book& a_book)
{
      return b_book.author_last_name < a_book.author_last_name ||
               (b_book.author_last_name == a_book.author_last_name &&
               b_book.author_first_name < a_book.author_first_name) ||
               (b_book.author_last_name == a_book.author_last_name &&
               b_book.author_first_name == a_book.author_first_name &&
               b_book.title < a_book.title);
}

//file name: book_user8.cpp

#include <list>
#include <string>
#include <fstream>
#include <iostream>
#include <algorithm>
#include "book8.h"

using namespace std;

void fill_list(list<Book>&, istream&);

template <class T>
inline void print_list(T& L, ostream& outfile, char* optional="")
{
      typename T::iterator pos;

      outfile << optional << endl;
      cout << optional << endl << endl;
      
      for (pos = L.begin(); pos != L.end(); ++pos)
      {
            outfile << *pos << ' ';
      }
      outfile << endl;
}

int main()
{
      list<Book> book_list;
      ifstream fin;
      ofstream fout;

      fin.open("C:\\CSC311\\Files\\book8_in.txt");
            
      if(fin.fail())
      {
            cout << "Failed to open input file" << endl;
            exit(1);
      }

      fout.open("C:\\CSC311\\Files\\book8_out.txt");

      if(fout.fail())
      {
            cout << "Failed to open output file"  << endl;
            exit(1);
      }

      fill_list(book_list, fin);
      print_list(book_list, fout, "Book List:\n");

    sort(book_list.begin(), book_list.end(), compare_last_name(b_book, a_book));
      print_list(book_list, fout, "Sorted Book List by Last Name:\n");
      
      fin.close();
      fout.close();      
      return 0;
}

void fill_list(list<Book>& book_list, istream& infile)
{
      Book a_book;

      while (!infile.eof())
      {
            infile >> a_book;
            book_list.push_back(a_book);
      }
}


0
Comment
Question by:deekakes
  • 4
  • 3
8 Comments
 
LVL 8

Assisted Solution

by:Exceter
Exceter earned 40 total points
ID: 9714900
Check this out for examples of using std::sort with a custom predicate function,
http://www.codeproject.com/vcpp/stl/stdsort.asp

Cheers!
Exceter
0
 
LVL 11

Expert Comment

by:bcladd
ID: 9714927
(1) STL list<> does not have random access iterators so you CANNOT use the stand alone sort algorithm. Instead you have to call list's sort:

  book_list.sort(compare_last_name);

Notice no parameter list; you're passing a pointer at the comparison function to sort.

Hope this helps, -bcl
0
 

Author Comment

by:deekakes
ID: 9715080
I added book_list.sort(compare_last_name) to the main program and the compiler states:

C:\CSC311\Files\book_user8.cpp(55) : error C2664: 'void __thiscall std::list<class Book,class std::allocator<class Book> >::sort(struct std::greater<class Book>)' : cannot convert parameter 1 from 'bool (const class Book &,const class Book &)' to 's
truct std::greater<class Book>'
        No constructor could take the source type, or constructor overload resolution was ambiguous
Error executing cl.exe.

book_user8.obj - 1 error(s), 0 warning(s)
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 11

Expert Comment

by:bcladd
ID: 9715245
What compiler are you using? I tested the line in Borland 5.5 (didn't have any data so I didn't run the resulting program). According to Josuttis' _The C++ Standard Library_ there are two versions of sort for list, one with no parameters and one with a single parameter, CompFunction op. It warns that the second form of sort is available only in systems that support member templates.

That is what I know.

-bcl
0
 

Author Comment

by:deekakes
ID: 9715465
I'm using VC++ 6.0.
0
 
LVL 11

Accepted Solution

by:
bcladd earned 60 total points
ID: 9715522
VC++ 6.0 does not supprot member templates so the notation I gave you will not work (guess you figured that part out on your own).

To sort a list you need to specialize std::greater for your class.  This question was also answered here before: http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_11073825.html

template<> class std::greater<class Book &>
{
   bool operator()(const Book & pFirst,
                          const Book & pSecond) const
   {
      return compare_last_name(pFirst, pSecond);
   }
};

Hope this helps, -bcl
0
 
LVL 11

Expert Comment

by:bcladd
ID: 9715558
Note: operator() must be public (put public: before the function declaration) and you may need to #include <functional> to make it work.

-bcl
0
 

Author Comment

by:deekakes
ID: 9719154
Thanks!
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question