C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old September 21st, 2008, 12:57 PM
tucsonspeed6 tucsonspeed6 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2008
Posts: 2 tucsonspeed6 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 m 19 sec
Reputation Power: 0
General - << operator troubles.

Bear with me here, this gets complicated...

I'm writing a program with multiple classes that also has multiple overloaded << operators. THe classes are templated so the final product will be a templated linked list that is given the type cClass. The cClass class is also templated and contains the title of a college CS course along with a tree of type cBSTree. cBSTree is also templated and is given the type cProfessor. cProfessor class contains two strings for the first and last name of the professor.

My problem is that I can't get any of this to output to the screen. Main is only given access to the linked list, which includes a "PrintList()" function.

PrintList() uses cout << cur->item; which does not compile because the << operator is "ambiguous". cClasses has a << operator that I don't know how to implement because even though it is given friendship, it doesn't have access to the private members of cClasses. cProfessor also has a << operator that as far as I know works just fine.

My question is how can I make all this print to the screen?

Below are the .h files, since the entire program is very long.


template <typename DataType>// class Must be templated
class cList
{
public:
cList(); // Constructor
cList(cList& copy); // Copy constructor
~cList(); // Destructor
cList operator =(cList& rhs); // Overloaded equal
void insert(const DataType element); // Inserts element into the list
bool isInList(DataType &element); // Determines if element is in the list
void PrintList(); // Prints the elements in the list to the screen
void PrintList(fstream& file); // Prints the elements in the list to the
// stream passed into the function

bool Delete(const DataType &toDelete); // Deletes element from the list
bool Delete(const DataType &toDelete, DataType &deleted); // Deletes an element from the list and passes the element
// from the list by reference (there are two areguments - what to delete
// and what was deleted
bool DeleteFirst(DataType &deleted); // Deletes the first element in the list and returns the element
// that was deleted

bool isEmpty(); // Determines if the list was empty

private:
void copyList(cList& toCopy);
// You may add private member functions if you want/need them

protected:
// You may NOT add and data members
struct node;
typedef node *nodePtr;
struct node
{
DataType info;
nodePtr next;
};
nodePtr head;
};


template <typename kind>// This is a templated class
class cClasses
{
public:

void setClassID(string Name);
// Sets the class name to the value passed in
string GetClassID();
// Returns the class name


// You may add any operators you may need
// the operators compare the class names
bool operator <(cClasses& rhs);
bool operator ==(const cClasses& rhs);
bool operator !=(cClasses& rhs);

void insert(kind toInsert); // Inserts a professor into the templated tree
bool Delete(kind toDelete); // Deletes a professor from the templated tree
void WriteProfstoFile(fstream &out); // Writes the professors in order to the
// stream that is passed into the function

template <typename kind>
friend ostream & operator <<(ostream out, cClasses<kind>& rhs); // Outputs the class name and all of the professors
// as a tree to the screen

private:
string ReturnClassName();
// can add any private functions you may need

protected:
string className; // Stores the class name - ex: CS140
cBSTree<kind> tree; // A templated binary search tree that contains the professors
// that teach the class

};


template <typename DataType>// class must be templated
class cBSTree
{
protected:
// You may NOT add any protect data members
struct node;
typedef node *nodePtr;

struct node
{
DataType element;
nodePtr right;
nodePtr left;
};

int count;
nodePtr root;

public:
cBSTree(); // Constructor
cBSTree(cBSTree& copy); // Copy Constructor
cBSTree operator =(cBSTree& rhs); // Equal Overload


~cBSTree(); // Destructor
void Insert(DataType element); // Inserts element into the tree
bool PrintTree(); // Prints the tree - as a tree - code is given below
void PrintInOrder(nodePtr cur); // Prints the tree in list order to the screen
void PrintInOrder(fstream& out, nodePtr cur); // Prints the tree in list order to the stream
// that was passed in
void PrintPreOrder(nodePtr cur); // Prints the tree in pre order to the screen
void PrintPreOrder(fstream& out, nodePtr cur); // Prints the tree in pre order to the stream
// that was passed to it


bool Delete(DataType item); // Deletes element from the tree
void DeleteTree(nodePtr rt); // Deletes the entire tree


private:
// You may add any private member functions you may need
void PrintTreeHelper(int depth, nodePtr cur) ; // Used by PrintTree - code is given below
void CopyTree(nodePtr rt, nodePtr copyRt);
bool DeleteNode(nodePtr& toDelete);
};



// Code is given to print the tree as a tree
// Functions must be templated and a member function

template <typename DataType>
bool cBSTree<DataType>::PrintTree()
{
if (root == NULL)
return false;
else
{
PrintTreeHelper(0, root);
cout << endl << endl;
return true;
}
}

// Functions must be templated and a member function
template <typename DataType>
void cBSTree<DataType>::PrintTreeHelper(int depth, nodePtr cur)
{
const int spacing = 4;
if (cur != NULL)
{
if (depth == 0)
cout << cur->element;
else
cout << setw(spacing*depth) <<" "<< cur->element;
cout << endl;
PrintTreeHelper(depth+1, cur->right);
PrintTreeHelper(depth+1, cur->left);
}
else
cout << setw(spacing*depth) <<" " << "****" << endl;

}


class cProfessor
{
public:
// You may add any operators you may need
// operators compare the professors' names
bool operator <=(cProfessor& rhs);
bool operator <(cProfessor& rhs);
bool operator >(cProfessor& rhs);
bool operator ==(cProfessor& rhs);

friend ostream & operator <<(ostream& out, cProfessor& rhs); // Prints the last name, a comma, and then the
// first name of the professor

void setLastName(string set); // Sets the last name to the string passes in
void setFirstName(string name); // Sets the first name to the string passed in


private:
// You may add any provate member functions you may need

protected:
string firstName; // for the first name
string lastName; // for the last name

};

Reply With Quote
  #2  
Old September 21st, 2008, 12:58 PM
tucsonspeed6 tucsonspeed6 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2008
Posts: 2 tucsonspeed6 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 m 19 sec
Reputation Power: 0
BTW: Thanks in advance for any help...

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > General - << operator troubles.


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap