I need help with my program. I have four errors with the ostream. It should be print my list of books. I know its something simple... I just can't figure it out!

#include <iostream>
#include <string>

using namespace std;
//----------------------------------------------------------------------------

class Book {
private:
        string title, author, genre, pubyear, publisher;
public:
        Book();                                                              
        Book(const Book& b);                                                 
        Book(string t, string a, string g, string py, string p);
      //  bool operator==(const Book& b);                                      
  //      bool compare==(const Book& b);
        
        
        string getTitle();                                                   
        string getAuthor();                                                  
        string getGenre();
        string getPubYear();
        string getPublisher();

        void set(string a, string t, string g, string py, string p); 
        void setTitle(string t); 
        void setAuthor(string a);
        void setGenre(string g);
        void setPubYear(string py);
        void setPublisher(string p);

        friend class Node;
        friend class List;                                
        friend istream& operator>>(istream& is, Book& b);
        friend ostream& operator<<(ostream& os, Book& b);
        friend ostream& operator<<(ostream&, const List&);
        
};



//----------------------------------------------------------------------------
class Node {
	Book data;
	Node *next;
	Node *prev;
	
public:
	Node() : data(), next(NULL) { }
	Node(Book b) : data(b), next(NULL) { }
	Node(Book b, Node* n) : data(b), next(n) { }
	Node(Book b, Node *n, Node *p) : data(b), next(n), prev(p) {}
	Node *getNext(void) const { return next; }
	Node *getPrev(void) const { return prev; }
	Book getData(void) const { return data; }
	void setData(Book b) { data = b; }
	void setNext(Node* n) { next = n; }
	void setPrev(Node* p) { prev = p; }
	friend ostream& operator<<(ostream&, const List&);
	friend class List;

	
	friend class Book;
};
//-----------------------------------------------------------------------------
class List {
	Node *start;
public:
	List();
	void push_front(Book);
	void push_back(Book);
	void pop_front(void);
	void pop_back();
	Node *find(Book);
	void insert(Node *, Book);
	
	friend class Book;
	friend ostream& operator<<(ostream&, const List&);
};
//------------------------------------------------------------------------------
// END OF CLASSES
//
//------------------------------------------------------------------------------

Book::Book():title(" "), author(" "), genre(" "), pubyear(" "), publisher(" "){}

Book::Book(string a, string t, string g, string py, string p) : author(a), title(t), genre(g), pubyear(py), publisher(p){}

Book::Book(const Book& b):title(b.title), author(b.author), genre(b.genre), pubyear(b.pubyear), publisher(b.publisher){}

string Book::getTitle(){
        return title;} 
string Book::getAuthor(){
        return author;}
string Book::getGenre(){
        return genre;}
string Book::getPubYear(){
        return pubyear;}
string Book::getPublisher(){
        return publisher;}

void Book::set(string a, string t, string g, string py, string p) {author=a, title=t, genre=g, pubyear=py, publisher=p;}

void Book::setTitle(string t){
        title=t;}                    
void Book::setAuthor(string a){
        author=a;}
void Book::setGenre(string g){
        genre=g;}
void Book::setPubYear(string py){
        pubyear=py;}
void Book::setPublisher(string p){
        publisher=p;}

istream& operator>>(istream& is, Book& b){ 
        string t, a, g, py, p;
        cout << endl << "Enter the following information for a book:" << endl;
        cout << "Title: ";
        getline(cin,t);
        b.setTitle(t);
        cout << "Author: ";
        getline(cin,a);
        b.setAuthor(a);
        cout << "Genre: ";
        getline(cin,g);
        b.setGenre(g);
        cout << "Publisher: ";
        getline(cin,p);
        b.setPublisher(p);
        cout << "Publication Year: ";
        getline(cin,py);
        b.setPubYear(py);
        cout << endl;
        return is;
}
/*bool Book::operator ==(const Book& b)
{
        if(author==b.author &&
                title==b.title &&
                genre==b.genre &&
                pubyear==b.pubyear &&
                publisher==b.publisher)
        {
                return true;
        }
        else
        {
                return false;
        }
}*/
/*bool compare::operator==(const Book b) {
     
     if(author==b.author &&
                title==b.title &&
                genre==b.genre &&
                pubyear==b.pubyear &&
                publisher==b.publisher)
        {
                return true;
        }
        else
        {
                return false;
        }
*/     
ostream& operator<<(ostream& os, Book& b){ 
        os << "Title: " <<  b.title << endl;
        os << "Author: " << b.author << endl;
        os << "Genre: " << b.genre << endl;
        os << "Publisher: " << b.publisher << endl;
        os << "Publication Year: " << b.pubyear << endl << endl;
        return os;
}
//------------------------------------------------------------------------------
/*Node *List::find(Book b) {
	Node *temp = start;
	while (temp) {
		
		if(temp.compare(b);
		return temp;
		
		
		temp = temp->getNext();
	}
	return 0;
}*/
//------------------------------------------------------------------------------
/*void List::insert(Node *ptr, Book b) {
	Node *insertNode = new Node(b,ptr->getNext());
	ptr->setNext(insertNode);

	return;
}*/
//------------------------------------------------------------------------------
ostream& operator<<(ostream& os, const List& l) {
	os << "< ";
	Node *temp = l.start;
	temp->getAuthor();
	
	while (temp) {
		os << temp->getData() << " ";
		os << temp->getAuthor();
		temp = temp->getNext();
	}
	os << ">";
	return os;
}

List::List() : start(NULL) { }

void List::push_front(Book b) {
	Node *newone = new Node(b,start);
	start = newone;
	return;
}
void List::push_back(Book) {

	Node *newone = new Node();
	if (start == NULL)
		start = newone;
	else {
		Node *temp = start;
		while (temp->getNext() != NULL)
			temp = temp->getNext();
		temp->setNext(newone);
	}
	return;
}

void List::pop_front(void){
	if (start != NULL) {
		Node *temp;
		temp = start->getNext();
		
		delete start; // free first element
		start = temp;
	}
}

void List::pop_back(){
	if (start != NULL) {
		Node *lag, *temp = start;
		while(temp->getNext() != NULL) {
			lag = temp;
			temp = temp->getNext();
		}
		delete temp;
		lag->setNext(NULL);
	}
}

//------------------------------------------------------------------------------
int main( ) {
//------------------------------------------------------------------------------
    Book b1, b2, b3("Tony Gaddis", "Starting out with Python", "CS", "2009", "Pearson-Addision-Wesley");
	b1.set("John Zelle", "Python Programming", "CS", "2004", "Franklin, Beedle & Associates" );
	b2.setAuthor("Glenn Brookshear");
	b2.setTitle("Computer Science: an Overview");
	b2.setGenre("CS");
	b2.setPubYear("2009");
	b2.setPublisher("Pearson-Addision-Wesley");
    Book b4(b2);
	cout << b4.getAuthor() << " " << b4.getTitle() << " " <<  b4.getGenre() << " " << b4.getPubYear()<< endl; 
	Book b5;
	cin >> b5;
	cout << b1 << b2 << b3 << b4 << b5;

    
    
//------------------------------------------------------------------------------                                                                            
	List a;
	a.push_back(b1);
	a.push_back(b2);
	a.push_back(b3);

	/*a.push_back(20);
	a.push_back(30);
	a.push_back(40);
	a.push_back(50);*/
	cout << a << endl;
	
//------------------------------------------------------------------------------
//	Node *temp;
//	temp = a.find(b2);
//	a.insert(temp, b3);
	/*
	temp = a.find(30);
	a.insert(temp, 35);
	temp = a.find(50);
	
	a.insert(temp, 1);*/
	cout << a << endl;
	/*
//------------------------------------------------------------------------------
	a.pop_front();
	cout << "After pop_front" << endl;
	cout << a << endl;
//------------------------------------------------------------------------------
	a.pop_back();
	cout << "After pop_back" << endl;
	cout << a << endl;
	*/
    int x;
    cin >> x;
}

Recommended Answers

All 9 Replies

ostream& operator<<(ostream& os, const List& l) {
	os << "< ";
	Node *temp = l.start;
	temp->getAuthor();
	
	while (temp) {
		os << temp->getData() << " ";
		os << temp->getAuthor();
		temp = temp->getNext();
	}
	os << ">";
	return os;
}

temp is of type Node* (see green above). The function in red above doesn't exist in the Node class, so you are getting errors.

class Node {
	Book data;
	Node *next;
	Node *prev;
	
public:
	Node() : data(), next(NULL) { }
	Node(Book b) : data(b), next(NULL) { }
	Node(Book b, Node* n) : data(b), next(n) { }
	Node(Book b, Node *n, Node *p) : data(b), next(n), prev(p) {}
	Node *getNext(void) const { return next; }
	Node *getPrev(void) const { return prev; }
	Book getData(void) const { return data; }
	void setData(Book b) { data = b; }
	void setNext(Node* n) { next = n; }
	void setPrev(Node* p) { prev = p; }
	friend ostream& operator<<(ostream&, const List&);
	friend class List;

	
	friend class Book;
};
//-----------------

I made Node friends with book so it should be able to use the getAuthor function...but it doesn't. How can I fix this?

I made Node friends with book so it should be able to use the getAuthor function...but it doesn't. How can I fix this?

Making classes friends of other classes means that classes can access private members of other classes. In your case, it's not a matter of public versus private, it's the fact that the function doesn't exist in the Node class. If you want to call getAuthor () from the Book class, create a Book object, not a Node object, and call the function using that:

Book book;
book.getAuthor ();

I made the changes those changes and now I have more errors! :(

Here is my updated code...

#include <iostream>
#include <string>

using namespace std;
//----------------------------------------------------------------------------

class Book {
private:
        string title, author, genre, pubyear, publisher;
public:
        Book();                                                              
        Book(const Book& b);                                                 
        Book(string t, string a, string g, string py, string p);
      //  bool operator==(const Book& b);                                      
  //      bool compare==(const Book& b);
        
        
        string getTitle();                                                   
        string getAuthor();                                                  
        string getGenre();
        string getPubYear();
        string getPublisher();

        void set(string a, string t, string g, string py, string p); 
        void setTitle(string t); 
        void setAuthor(string a);
        void setGenre(string g);
        void setPubYear(string py);
        void setPublisher(string p);

        friend class Node;
        friend class List;                                
        friend istream& operator>>(istream& is, Book& b);
        friend ostream& operator<<(ostream& os, Book& b);
        friend ostream& operator<<(ostream&, const List&);
        
};



//----------------------------------------------------------------------------
class Node {
	Book data;
	Node *next;
	Node *prev;
	
public:
	Node() : data(), next(NULL) { }
	Node(Book b) : data(b), next(NULL) { }
	Node(Book b, Node* n) : data(b), next(n) { }
	Node(Book b, Node *n, Node *p) : data(b), next(n), prev(p) {}
	Node *getNext(void) const { return next; }
	Node *getPrev(void) const { return prev; }
	Book getData(void) const { return data; }
	void setData(Book b) { data = b; }
	void setNext(Node* n) { next = n; }
	void setPrev(Node* p) { prev = p; }
	friend ostream& operator<<(ostream&, const List&);
	friend class List;

	
	friend class Book;
};
//-----------------------------------------------------------------------------
class List {
	Node *start;
public:
	List();
	void push_front(Book);
	void push_back(Book);
	void pop_front(void);
	void pop_back();
	Node *find(Book);
	void insert(Node *, Book);
	
	friend class Book;
	friend ostream& operator<<(ostream&, const List&);
};
//------------------------------------------------------------------------------
// END OF CLASSES
//
//------------------------------------------------------------------------------

Book::Book():title(" "), author(" "), genre(" "), pubyear(" "), publisher(" "){}

Book::Book(string a, string t, string g, string py, string p) : author(a), title(t), genre(g), pubyear(py), publisher(p){}

Book::Book(const Book& b):title(b.title), author(b.author), genre(b.genre), pubyear(b.pubyear), publisher(b.publisher){}

string Book::getTitle(){
        return title;} 
string Book::getAuthor(){
        return author;}
string Book::getGenre(){
        return genre;}
string Book::getPubYear(){
        return pubyear;}
string Book::getPublisher(){
        return publisher;}

void Book::set(string a, string t, string g, string py, string p) {author=a, title=t, genre=g, pubyear=py, publisher=p;}

void Book::setTitle(string t){
        title=t;}                    
void Book::setAuthor(string a){
        author=a;}
void Book::setGenre(string g){
        genre=g;}
void Book::setPubYear(string py){
        pubyear=py;}
void Book::setPublisher(string p){
        publisher=p;}

istream& operator>>(istream& is, Book& b){ 
        string t, a, g, py, p;
        cout << endl << "Enter the following information for a book:" << endl;
        cout << "Title: ";
        getline(cin,t);
        b.setTitle(t);
        cout << "Author: ";
        getline(cin,a);
        b.setAuthor(a);
        cout << "Genre: ";
        getline(cin,g);
        b.setGenre(g);
        cout << "Publisher: ";
        getline(cin,p);
        b.setPublisher(p);
        cout << "Publication Year: ";
        getline(cin,py);
        b.setPubYear(py);
        cout << endl;
        return is;
}
/*bool Book::operator ==(const Book& b)
{
        if(author==b.author &&
                title==b.title &&
                genre==b.genre &&
                pubyear==b.pubyear &&
                publisher==b.publisher)
        {
                return true;
        }
        else
        {
                return false;
        }
}*/
/*bool compare::operator==(const Book b) {
     
     if(author==b.author &&
                title==b.title &&
                genre==b.genre &&
                pubyear==b.pubyear &&
                publisher==b.publisher)
        {
                return true;
        }
        else
        {
                return false;
        }
*/     
ostream& operator<<(ostream& os, Book& b){ 
        os << "Title: " <<  b.title << endl;
        os << "Author: " << b.author << endl;
        os << "Genre: " << b.genre << endl;
        os << "Publisher: " << b.publisher << endl;
        os << "Publication Year: " << b.pubyear << endl << endl;
        return os;
}
//------------------------------------------------------------------------------
/*Node *List::find(Book b) {
	Node *temp = start;
	while (temp) {
		
		if(temp.compare(b);
		return temp;
		
		
		temp = temp->getNext();
	}
	return 0;
}*/
//------------------------------------------------------------------------------
/*void List::insert(Node *ptr, Book b) {
	Node *insertNode = new Node(b,ptr->getNext());
	ptr->setNext(insertNode);

	return;
}*/
//------------------------------------------------------------------------------
ostream& operator<<(ostream& os, const List& l) {
         
	os << "< ";
	Node *temp = l.start;
	Book *temp2;
	temp2->getAuthor();
	
	while (temp) {
      Node *temp;
		os << temp->getData() << " ";
		os << temp2->getAuthor();
		temp = temp->getNext();
	}
	os << ">";
	return os;
}

List::List() : start(NULL) { }

void List::push_front(Book b) {
	Node *newone = new Node(b,start);
	start = newone;
	return;
}
void List::push_back(Book) {

	Node *newone = new Node();
	if (start == NULL)
		start = newone;
	else {
		Node *temp = start;
		while (temp->getNext() != NULL)
			temp = temp->getNext();
		temp->setNext(newone);
	}
	return;
}

void List::pop_front(void){
	if (start != NULL) {
		Node *temp;
		temp = start->getNext();
		
		delete start; // free first element
		start = temp;
	}
}

void List::pop_back(){
	if (start != NULL) {
		Node *lag, *temp = start;
		while(temp->getNext() != NULL) {
			lag = temp;
			temp = temp->getNext();
		}
		delete temp;
		lag->setNext(NULL);
	}
}

//------------------------------------------------------------------------------
int main( ) {
//------------------------------------------------------------------------------
    Book b1, b2, b3("Tony Gaddis", "Starting out with Python", "CS", "2009", "Pearson-Addision-Wesley");
	b1.set("John Zelle", "Python Programming", "CS", "2004", "Franklin, Beedle & Associates" );
	b2.setAuthor("Glenn Brookshear");
	b2.setTitle("Computer Science: an Overview");
	b2.setGenre("CS");
	b2.setPubYear("2009");
	b2.setPublisher("Pearson-Addision-Wesley");
    Book b4(b2);
	cout << b4.getAuthor() << " " << b4.getTitle() << " " <<  b4.getGenre() << " " << b4.getPubYear()<< endl; 
	Book b5;
	cin >> b5;
	cout << b1 << b2 << b3 << b4 << b5;

    
    
//------------------------------------------------------------------------------                                                                            
	List a;
	a.push_back(b1);
	a.push_back(b2);
	a.push_back(b3);

	/*a.push_back(20);
	a.push_back(30);
	a.push_back(40);
	a.push_back(50);*/
	cout << a << endl;
	
//------------------------------------------------------------------------------
//	Node *temp;
//	temp = a.find(b2);
//	a.insert(temp, b3);
	/*
	temp = a.find(30);
	a.insert(temp, 35);
	temp = a.find(50);
	
	a.insert(temp, 1);*/
	cout << a << endl;
	/*
//------------------------------------------------------------------------------
	a.pop_front();
	cout << "After pop_front" << endl;
	cout << a << endl;
//------------------------------------------------------------------------------
	a.pop_back();
	cout << "After pop_back" << endl;
	cout << a << endl;
	*/
    int x;
    cin >> x;
}

I declare the ostream and I don't know what to do know.

I think this problem is a scoping issue and that temp->getData () immediately goes out of scope before it can be displayed:

os << temp->getData() << " ";

Try replacing it with this:

Book aBook = temp->getData ();
os << aBook << " ";

Making that change made it compile for me.

Okay sorry for the double post...I'll go try that. Thank you so much!

After I did that my compiler stopped working. I tried it on Dev C++ and Microsoft Visual C++2008. Both of them stopped working. :(

After I did that my compiler stopped working. I tried it on Dev C++ and Microsoft Visual C++2008. Both of them stopped working. :(

The compiler stopped working? Did it crash the whole system? So you compiled and ran it or just compiled it? If you ran it, my guess is that there is an infinite loop, memory leak, something like that, and it has to do with your compiled program, not the compiler. Best bet is to simply build it/compile it, but don't execute it. Then try executing it, but perhaps execute it OUTSIDE of your IDE (Vis. Studio or Dev C++) by either running it from command line or even just double clicking the executable. Name it something memorable and before running it, open the Windows Task Manager. Look under the Performance tab and see if you get a CPU Usage spike. You can also look under the Processes tab (again, remember the name) and see if you have a very large Memory Usage for that program. Also look under the Applications tab and see whether it's not responding. If any of these things are happening, time to debug with the debugger, step through the program, all that fun stuff. But it should at least compile.

After I did that my compiler stopped working. I tried it on Dev C++ and Microsoft Visual C++2008. Both of them stopped working. :(

Could you post me the whole code which broke your compiler ?
I would like to try it :twisted: ...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.