| | |
Struct within Class, Problem with OVERLOADING OPERATORS
![]() |
•
•
Join Date: Feb 2009
Posts: 11
Reputation:
Solved Threads: 0
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:
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)
#include <iostream> #include <cstring> #include <string> #include <cstddef>//for NULL 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(); bool sortedIsEmpty()const; int sortedGetLength()const; void sortedInsert(string newWord); void sortedRemove(string oldWord); void sortedRetrieve(int index, string& theWord)const; friend bool operator==(const SortedListNode& wordA, const 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 } bool 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) //} } } bool SortedList::operator==(const SortedListNode*& wordA, const SortedListNode*& wordB) { string first = wordA->word; string second = wordB->word; const char* forFirst = first.c_str(); const char* forSecond = second.c_str(); return strcmpi(forFirst, forSecond); }
Last edited by moshe5; Mar 13th, 2009 at 9:20 pm.
1) get rid of strcmpi() C function. See my version of the == operator below.
2) add
3) The friend function is not part of the class, so it has to reference the SortedListNode by SortedList:
ortedListNode
2) add
#include <algorithm> for the transform() function3) 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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Feb 2009
Posts: 11
Reputation:
Solved Threads: 0
•
•
•
•
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; }
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
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
Similar Threads
- ++ Operator Overloading (C++)
- a small prob with operator overloading (C++)
- problem with operator overloading (C++)
Other Threads in the C++ Forum
- Previous Thread: importing images
- Next Thread: Change text color using visual c++
| Thread Tools | Search this Thread |
api application array based binary bitmap c# c++ c/c++ char class classes code coding compile compression console conversion count cpm delete deploy deque desktop developer dialog directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer introductory java lib linkedlist linkednodes linker loop looping loops map math matrix memory multiple news node numbertoword output parameter pointer problem program programming project python random read recursion reference rpg security sorting string strings temperature template test text text-file tree url variable vector video whyisthiscodecausingsegmentationfault win32 windows winsock wordfrequency wxwidgets






