Passing Class arrays to Friend Functions


Hi. I am having a terrible problem and I can not solve and hope that someone here can help me.

I have a program that has several class decelerations at the top and a FRIEND function to the class.

The calls to the class members work fine but when I try to call the friend function I get all kinds of errors. I have not been able to locate any code similar to this anywhere.

Basically, I have the user enter 5 book titles and then create objects out of them. Then I display them. No problem. The problem comes in is when I try to call the sort() function to sort them in alphabetical order. No matter what I try, I can not get this to work.

Please Help!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <iomanip>
#include <stdio.h>
#include <cmath>
#include <sstream>

using namespace std;

// class declaration section

class Book
{

  protected:
          char *title;   //a pointer to a book title

  public:   
         Book( char * = '\0');  //constructor 
         Book(Book&);
         ~Book();
         void showTitle( );  
         void operator=(Book& );
         void setTitle(char * );

      friend void sort(Book list[] , int );
};

//Class implementation section
Book::Book(char *strng)
{
  if(strng)
  {
	title = new char[strlen(strng)+1];  // allocate memory
	strcpy(title,strng);                // store the string
  }
  else
	  title = NULL;
}

Book::Book(Book& oldbook)
{
  title = new char[strlen(oldbook.title) + 1];  // allocate new memory
  strcpy(title, oldbook.title);  // copy the title
}

void Book::operator=(Book& oldbook)
{
 if (oldbook.title !=NULL)  //check to see if there is already one there
      delete(title);     //delete it if so
     title = new char[strlen(oldbook.title)+1];
     strcpy(title,oldbook.title);                    

}

void Book::showTitle(void)
{
     cout << title << endl;
return;

}

void Book::setTitle(char *strng)
{
  title = new char[strlen(strng)+1];  // allocate memory
  strcpy(title,strng);                // store the string

  return;
}
  Book::~Book()
{
  delete(title);
  title = NULL;

}
 
int main()
{
 const int MAXCHARS = 100;
 Book book[5];

 char title[MAXCHARS]; 
 int numbooks = 5, i ;


for (i = 0 ; i < numbooks; i++)
{
       cout << "Enter book #  " << i+1 << " " ;
      cin.getline(title, MAXCHARS);
	  book[i].setTitle(title);
}

//************** This is the pass to the sort that does not work
sort( book[ ]  ,numbooks);

for (i = 0 ; i < numbooks; i++)
{
    book[i].showTitle();
}

cout << "Press ENTER to continue...";
cin.ignore( numeric_limits<streamsize>::max(), '\n' );
  return  EXIT_SUCCESS;
}

//function section 
//sort function
void sort(Book* list[], int numElements)
{
  int i;
  Book* temp;
  int outord = 1;

  while (outord && (numElements > 0))
  {
    outord = 0;
    for (i = 0; i < (numElements - 1); i++)
    {
      if (strcmp(list[i]->title, list[i+1]->title) > 0)
      {
        temp = list[i+1];
        list[i+1] = list[i];
        list[i] = temp;
        outord = 1;
      }
    }
  numElements--;
  }

  return;
}
I see no reasion that you get the error due to friend function ..
what is the error you are getting ?
Its not the friend function that is giving the error... It is the call and passage of data to the sort() function... It bombs on that line and says that the types are incompatible
closed account (DSLq5Di1)
1
2
3
4
5
6
7
int main()
{
  ...

  //************** This is the pass to the sort that does not work
  sort( book[ ]  ,numbooks);
  sort( book  ,numbooks);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void sort(Book* list[], int numElements) // inconsistent with your friend declaration
void sort(Book list[], int numElements)
{
  int i;
  Book* temp;
  Book temp;
  int outord = 1;

  while (outord && (numElements > 0))
  {
    outord = 0;
    for (i = 0; i < (numElements - 1); i++)
    {
      if (strcmp(list[i]->title, list[i+1]->title) > 0)
      if (strcmp(list[i].title, list[i+1].title) > 0)
      {
Thanks Sloopy.... That seemed to work. I guess I missed the passing of the * part which made it inconsistent.


Thanks again.
Topic archived. No new replies allowed.