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.
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <iomanip>
#include <stdio.h>
#include <cmath>
#include <sstream>
usingnamespace 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( );
voidoperator=(Book& );
void setTitle(char * );
friendvoid sort(Book list[] , int );
};
//Class implementation section
Book::Book(char *strng)
{
if(strng)
{
title = newchar[strlen(strng)+1]; // allocate memory
strcpy(title,strng); // store the string
}
else
title = NULL;
}
Book::Book(Book& oldbook)
{
title = newchar[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 = newchar[strlen(oldbook.title)+1];
strcpy(title,oldbook.title);
}
void Book::showTitle(void)
{
cout << title << endl;
return;
}
void Book::setTitle(char *strng)
{
title = newchar[strlen(strng)+1]; // allocate memory
strcpy(title,strng); // store the string
return;
}
Book::~Book()
{
delete(title);
title = NULL;
}
int main()
{
constint 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;
}
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
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)
{