943,803 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3645
  • C++ RSS
Mar 13th, 2009
0

Struct within Class, Problem with OVERLOADING OPERATORS

Expand Post »
So the assignment is to create a Sorted Linked List, not doubly, and not using a tail. I am using a SortedListNode Struct in the private area of the SortedList class. I think this is causing problems. I have the logic, and I was on the verge of succeeding, but then I got stuck with this. I never had to overload == or < or > for a Struct item, within a Class. (and one main requirement is that I do not use a helper function within my insert or remove functions)

The SortedList will eventually accept a string word, and compare it (while doing insert) to the cur one, to determain where to put it. (for this I am converting it to char, and then doing strcmpi)
but the main problem right now is that I think I don't know how to overload the Struct within the Class as a friend. I know there is more than one option, and not use a friend, but I will probably encounter the same problem with the syntax.

Here is my code:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <cstring>
  3. #include <string>
  4. #include <cstddef>//for NULL
  5. using namespace std;
  6.  
  7. class SortedList
  8. {
  9. private:
  10.  
  11. struct SortedListNode
  12. {
  13. string word;//holds within the word in the node
  14. SortedListNode* next;//pointer to next node
  15. };
  16.  
  17. int size;//number of nodes (words) in the sorted list
  18.  
  19. SortedListNode* head;//pointer to the sorted linked list of words
  20.  
  21. //optional: SortedListNode* tail;//pointer to th
  22.  
  23. //SortedListNode* locatePosition(string aWord,
  24.  
  25. public:
  26.  
  27. SortedList();
  28.  
  29. ~SortedList();
  30.  
  31. bool sortedIsEmpty()const;
  32.  
  33. int sortedGetLength()const;
  34.  
  35. void sortedInsert(string newWord);
  36.  
  37. void sortedRemove(string oldWord);
  38.  
  39. void sortedRetrieve(int index, string& theWord)const;
  40.  
  41. friend bool operator==(const SortedListNode& wordA, const SortedListNode& wordB);
  42. };
  43.  
  44. SortedList::SortedList(): size(0), head(NULL)
  45. {}
  46.  
  47. SortedList::~SortedList()
  48. {
  49. delete head;//and not to forget, use size to walk through the sorted list and delete all Nodes
  50. }
  51.  
  52. bool SortedList::sortedIsEmpty() const
  53. {
  54. return size;//if size is 0, it returns 0, if size is any other number- it returns 1
  55. }
  56.  
  57. int SortedList::sortedGetLength()const
  58. {
  59. return size;
  60. }
  61.  
  62. void SortedList::sortedInsert(string newWord)
  63. {
  64. SortedListNode* newNodePtr = new SortedListNode;
  65. newNodePtr->next = NULL;
  66. newNodePtr->word = newWord;
  67.  
  68. if(size==0)//insertion at beginning of List
  69. {
  70. newNodePtr->next = head;
  71. head = newNodePtr;
  72. size++;
  73. }
  74. else//insertion not at beginning of list
  75. {
  76. SortedListNode* prev = NULL;
  77. SortedListNode* cur = head;
  78. //for(; cur->next != NULL; prev = cur, cur = cur->next);//for(int i = size; i <= size; i++)
  79. //{
  80. //if (newNodePtr->word == cur->word); //throw exception- already present
  81.  
  82. //if (newNodePtr->word > cur->word)
  83. //{
  84. // if(cur->next = NULL);// then its the last one on list- insert here
  85. // }
  86. //else
  87. //{
  88. //advance by 1 more node
  89. // }
  90.  
  91. //if newNodePtr->word < cur->word then prev->next = newNodePtr and newNodePtr->next = cur (insert new node here)
  92.  
  93.  
  94.  
  95. //}
  96. }
  97. }
  98.  
  99. bool SortedList::operator==(const SortedListNode*& wordA, const SortedListNode*& wordB)
  100. {
  101. string first = wordA->word;
  102. string second = wordB->word;
  103. const char* forFirst = first.c_str();
  104. const char* forSecond = second.c_str();
  105. return strcmpi(forFirst, forSecond);
  106. }
Last edited by moshe5; Mar 13th, 2009 at 9:20 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
moshe5 is offline Offline
11 posts
since Feb 2009
Mar 13th, 2009
0

Re: Struct within Class, Problem with OVERLOADING OPERATORS

1) get rid of strcmpi() C function. See my version of the == operator below.
2) add #include <algorithm> for the transform() function

3) The friend function is not part of the class, so it has to reference the SortedListNode by SortedList:ortedListNode
#include <iostream>
#include <cstring>
#include <string>
#include <cstddef>//for NULL
#include <algorithm>
using namespace std;

class SortedList
{
private:
	
	struct SortedListNode
	{
		string word;//holds within the word in the node
		SortedListNode* next;//pointer to next node
	};
	
	int size;//number of nodes (words) in the sorted list
	
	SortedListNode* head;//pointer to the sorted linked list of words

	//optional: SortedListNode* tail;//pointer to th
	
	//SortedListNode* locatePosition(string aWord, 

public:
	
	SortedList();
	
	~SortedList();

	int sortedIsEmpty()const;

	int sortedGetLength()const;
	
	void sortedInsert(string newWord);

	void sortedRemove(string oldWord); 
	
	void sortedRetrieve(int index, string& theWord)const;

// Note:  if you want to pass pointers to this function
// then change it to * instead of &, but not both.
    friend bool operator==(const SortedList::SortedListNode& wordA, const SortedList::SortedListNode& wordB); 
};

SortedList::SortedList(): size(0), head(NULL)
{}

SortedList::~SortedList() 
{
	delete head;//and not to forget, use size to walk through the sorted list and delete all Nodes
}

int SortedList::sortedIsEmpty() const
{
    return size;//if size is 0, it returns 0, if size is any other number- it returns 1
}

int SortedList::sortedGetLength()const
{
	return size;
}

void SortedList::sortedInsert(string newWord)
{
	SortedListNode* newNodePtr = new SortedListNode;
	newNodePtr->next = NULL;
	newNodePtr->word = newWord;
	
	if(size==0)//insertion at beginning of List
	{
		newNodePtr->next = head;
		head = newNodePtr;
		size++;
	}
	else//insertion not at beginning of list
	{
		SortedListNode* prev = NULL;
		SortedListNode* cur = head;
		//for(; cur->next != NULL; prev = cur, cur = cur->next);//for(int i = size; i <= size; i++)
		//{ 
			//if (newNodePtr->word == cur->word); //throw exception- already present

			//if (newNodePtr->word > cur->word) 
			//{
			//	if(cur->next = NULL);// then its the last one on list- insert here 
		//	}
			//else
			//{
				//advance by 1 more node
		//	}
			
			//if newNodePtr->word < cur->word then prev->next = newNodePtr and newNodePtr->next = cur (insert new node here)
		

	
		//}
	}
}


// Note:  See my node in the class above.
bool operator==(const SortedList::SortedListNode& wordA, const SortedList::SortedListNode& wordB)
{
    string a = wordA.word;
    string b = wordB.word;
    transform(a.begin(), a.end(), a.begin(), toupper);
    transform(b.begin(), b.end(), b.begin(), toupper);
    return a == b ? true : false;
}
Last edited by Ancient Dragon; Mar 13th, 2009 at 11:42 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Mar 14th, 2009
0

Re: Struct within Class, Problem with OVERLOADING OPERATORS

1) get rid of strcmpi() C function. See my version of the == operator below.
2) add #include <algorithm> for the transform() function

3) The friend function is not part of the class, so it has to reference the SortedListNode by SortedList:ortedListNode
#include <iostream>
#include <cstring>
#include <string>
#include <cstddef>//for NULL
#include <algorithm>
using namespace std;

class SortedList
{
private:
	
	struct SortedListNode
	{
		string word;//holds within the word in the node
		SortedListNode* next;//pointer to next node
	};
	
	int size;//number of nodes (words) in the sorted list
	
	SortedListNode* head;//pointer to the sorted linked list of words

	//optional: SortedListNode* tail;//pointer to th
	
	//SortedListNode* locatePosition(string aWord, 

public:
	
	SortedList();
	
	~SortedList();

	int sortedIsEmpty()const;

	int sortedGetLength()const;
	
	void sortedInsert(string newWord);

	void sortedRemove(string oldWord); 
	
	void sortedRetrieve(int index, string& theWord)const;

// Note:  if you want to pass pointers to this function
// then change it to * instead of &, but not both.
    friend bool operator==(const SortedList::SortedListNode& wordA, const SortedList::SortedListNode& wordB); 
};

SortedList::SortedList(): size(0), head(NULL)
{}

SortedList::~SortedList() 
{
	delete head;//and not to forget, use size to walk through the sorted list and delete all Nodes
}

int SortedList::sortedIsEmpty() const
{
    return size;//if size is 0, it returns 0, if size is any other number- it returns 1
}

int SortedList::sortedGetLength()const
{
	return size;
}

void SortedList::sortedInsert(string newWord)
{
	SortedListNode* newNodePtr = new SortedListNode;
	newNodePtr->next = NULL;
	newNodePtr->word = newWord;
	
	if(size==0)//insertion at beginning of List
	{
		newNodePtr->next = head;
		head = newNodePtr;
		size++;
	}
	else//insertion not at beginning of list
	{
		SortedListNode* prev = NULL;
		SortedListNode* cur = head;
		//for(; cur->next != NULL; prev = cur, cur = cur->next);//for(int i = size; i <= size; i++)
		//{ 
			//if (newNodePtr->word == cur->word); //throw exception- already present

			//if (newNodePtr->word > cur->word) 
			//{
			//	if(cur->next = NULL);// then its the last one on list- insert here 
		//	}
			//else
			//{
				//advance by 1 more node
		//	}
			
			//if newNodePtr->word < cur->word then prev->next = newNodePtr and newNodePtr->next = cur (insert new node here)
		

	
		//}
	}
}


// Note:  See my node in the class above.
bool operator==(const SortedList::SortedListNode& wordA, const SortedList::SortedListNode& wordB)
{
    string a = wordA.word;
    string b = wordB.word;
    transform(a.begin(), a.end(), a.begin(), toupper);
    transform(b.begin(), b.end(), b.begin(), toupper);
    return a == b ? true : false;
}
I tried, but it says:
1>MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
1>C:\Users\Owner\Documents\Visual Studio 2008\Projects\Project1\testing\Debug\testing.exe : fatal error LNK1120: 1 unresolved externals
Reputation Points: 10
Solved Threads: 0
Newbie Poster
moshe5 is offline Offline
11 posts
since Feb 2009
Mar 14th, 2009
0

Re: Struct within Class, Problem with OVERLOADING OPERATORS

Hey, its your code, not mine. Where is the main() function ??? Didn't your teacher tell you that every c++ program needs a main() function.
Last edited by Ancient Dragon; Mar 14th, 2009 at 12:21 am.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Mar 14th, 2009
0

Re: Struct within Class, Problem with OVERLOADING OPERATORS

oh my god! LOL I have it, but I forgot to put it in when I used your solution. Im checking it, and soon ill post. THANKS!!!!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
moshe5 is offline Offline
11 posts
since Feb 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: importing images
Next Thread in C++ Forum Timeline: Recursion in C++





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC