This post has been edited by thefitgeek: 06 July 2010 - 01:19 PM
overloading << with classeswriting a program to output the information stored in a node of a LL
Page 1 of 1
8 Replies - 928 Views - Last Post: 06 July 2010 - 08:36 PM
#1
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?
Replies To: overloading << with classes
#2
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"...
#3
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
I'm calling it from my list class using the following code:
based on examples I have it should work but I have very limited experience with operator overloading.
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
#4
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?
Can we see the class declaration?
#5
Re: overloading << with classes
Posted 06 July 2010 - 07:51 AM
Probably a const issue. Also, you're not assigning a value.
Try this:
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;
}
}
#6
Re: overloading << with classes
Posted 06 July 2010 - 01:21 PM
Here's the declaration for the list class:
and the declaration for the winery 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_
#7
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.
#8
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:
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.
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.
#9
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.
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote




|