overloading << with classes

writing a program to output the information stored in a node of a LL

Page 1 of 1

8 Replies - 1262 Views - Last Post: 06 July 2010 - 08:36 PM Rate Topic: -----

#1 thefitgeek  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 11-February 10

overloading << with classes

Posted 05 July 2010 - 04:21 PM

I'm trying to write a program with two classes, the linked list is the implementation for the winery class. Winery class includes the name of the winery, location, average price, and rating. I'm supposed to use the code as written for me and make my functions/code suit what's written. No big deal I thought, until I tried getting outputting the value of each node using the friend function. I'm trying to output the data from the list.cpp file and the overloaded friend function is in the restaurant class. Anyone have any tips or examples for this?

This post has been edited by thefitgeek: 06 July 2010 - 01:19 PM


Is This A Good Question/Topic? 0
  • +

Replies To: overloading << with classes

#2 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: overloading << with classes

Posted 05 July 2010 - 04:32 PM

We're not going to hand you code if that's what you mean by "tips"...
Was This Post Helpful? 1
  • +
  • -

#3 thefitgeek  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 11-February 10

Re: overloading << with classes

Posted 06 July 2010 - 06:43 AM

That's not what I was asking for, just giving me code won't help me understand what my code needs to be doing.

here's the code in my winery class
ostream& operator<<(ostream& out, winery *w)
{
	// code to set column width and define output
	out << 	out.width(26) << left << w->name
		<< out.width(18) << left << w->location
		<< out.width(5) << left << w->acres
		<< out.width(6) << left << w->rating;
	
	return out;
}



I'm calling it from my list class using the following code:


void list::displayByName(ostream& out) const
{
	// your code here
	list::node* curr;

	curr->item.displayHeadings(out);
	for(curr = headByName; curr; curr = curr->nextByName)
		out << *curr << endl;

}



based on examples I have it should work but I have very limited experience with operator overloading.

This post has been edited by thefitgeek: 06 July 2010 - 01:19 PM

Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3106
  • View blog
  • Posts: 19,145
  • Joined: 14-September 07

Re: overloading << with classes

Posted 06 July 2010 - 06:50 AM

The second parameter in the << overload should be a reference.

Can we see the class declaration?
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5874
  • View blog
  • Posts: 12,752
  • Joined: 16-October 07

Re: overloading << with classes

Posted 06 July 2010 - 07:51 AM

Probably a const issue. Also, you're not assigning a value.

Try this:

ostream& operator<<(ostream& out, const winery &w) {
	// code to set column width and define output
	out << 	out.width(26) << left << w.name
		<< out.width(18) << left << w.location
		<< out.width(5) << left << w.acres
		<< out.width(6) << left << w.rating;
	
	return out;
}

void list::displayByName(ostream& out) const {
	if (headByName==NULL) { 
		out << "List is empty." << endl;
		return;
	}
	
	// you need to initialize this before you can all it, yest
	list::node* curr = headByName;

	curr->item.displayHeadings(out);
	while(curr!=NULL) { // sorry, for loop was confusing
		out << *curr << endl;
		curr = curr->nextByName;
	}

}


Was This Post Helpful? 1
  • +
  • -

#6 thefitgeek  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 11-February 10

Re: overloading << with classes

Posted 06 July 2010 - 01:21 PM

Here's the declaration for the list class:

#ifndef _LIST_
#define _LIST_

#include <ostream>
#include "winery.h"

using namespace std;

class list
{
public:
	list(void);				// constructor
	virtual ~list(void);	// destructor
	void displayByName(ostream& out) const;
	void displayByRating(ostream& out) const;
	void insert(const winery& winery);
	winery * const find(const char * const name) const;
		// Returns a const pointer to the winery instance it found in
		// the list, or 0 if it didn't find a winery with that name.
		// Because the pointer is declared const, there is no danger
		// that find's caller will be able to use the returned pointer
		// to change the instance of winery.
	bool remove(const char * const name);

private:
	struct node
	{
		node(const winery& winery);		// constructor
		winery item;
		node * nextByName;
		node * nextByRating;
	};

	node * headByName;
	node * headByRating;
};

#endif // _LIST_



and the declaration for the winery class:

#ifndef _WINERY_
#define _WINERY_

#include <ostream>

class winery
{
public:
	winery(const char * const name, const char * const location, const int acres, const int rating);
	virtual ~winery(void);
	const char * const getName() const;
	const char * const getLocation() const;
	const int getAcres() const;
	const int getRating() const;

	// display headings for lists of wineries
	static void displayHeadings(std::ostream& out);

	friend std::ostream& operator<<(std::ostream& out, winery *w);

private:
	char	*name;
	char	*location;
	int		acres;
	int		rating;
};

#endif // _WINERY_


Was This Post Helpful? 0
  • +
  • -

#7 thefitgeek  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 11-February 10

Re: overloading << with classes

Posted 06 July 2010 - 02:19 PM

baavgai: I tried the code but still receive the error of trying to convert pointers. I'm supposed to use the code as written so overloading should use a pointer (as assigned), but I'm currently rewriting my code and thinking I'll ignore that idea and just pass it the address for the winery object.
Was This Post Helpful? 0
  • +
  • -

#8 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5874
  • View blog
  • Posts: 12,752
  • Joined: 16-October 07

Re: overloading << with classes

Posted 06 July 2010 - 04:10 PM

It's probably not the output. Test a single "winery" object and see how it spins. Using C strings is a very bad idea, but if that's what you're stuck with...

This runs:
#include <iostream>
#include <cstring>

using namespace std;

class winery {
public:
	winery(const char * const name, const char * const location, const int acres, const int rating);
	virtual ~winery(void) {
		delete [] name;
		delete [] location;
	}

	friend std::ostream& operator<<(std::ostream& out, winery *w);

private:
	char *name, *location;
	int acres, rating;
};

winery::winery(const char * const name, const char * const location, const int acres, const int rating) {
	
	this->name = strdup(name);
	this->location = strdup(location);
	this->acres = acres;
	this->rating = rating;
}

std::ostream& operator<<(std::ostream& out, winery *w) {
	out << w->name << '\t' << w->location 
		<< '\t' << w->acres << '\t' << w->rating;
	return out;
}


int main() {
	winery w("Grapeville", "CA", 10, 7);
	winery w2("Honeydale", "PN", 60, 8);
	cout << &w << endl;
	cout << &w2 << endl;
	cout << "done." << endl;
	return 0;
}



Note, strdup is a GNU function, so this works on linux. You'll have to write your own in some lesser OS. ;)

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#9 thefitgeek  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 11-February 10

Re: overloading << with classes

Posted 06 July 2010 - 08:36 PM

Thank you! I haven't had the opportunity to try anything yet but at the very least you've got my brain working and I feel a hell less like my head is pounding against a brick wall. I'll post my progress in the morning.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1