| | |
Linked List with Char Array problem
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Apr 2009
Posts: 3
Reputation:
Solved Threads: 0
Hey All,
I'm having this weird issue and I'm not sure what to do. In the main function when I put one letter it'll work, once I put a string of letters it doesn't work. I've tried an assortment of stuff and I dunno what to do anymore.
This is the code that works when I put one letter in. Right now in the main it has "first", "second", and "third" and I get error C2664: cannot convert parameter 1 from 'const char[6]' to 'char'.
Thanks in advanced.
I'm having this weird issue and I'm not sure what to do. In the main function when I put one letter it'll work, once I put a string of letters it doesn't work. I've tried an assortment of stuff and I dunno what to do anymore.
This is the code that works when I put one letter in. Right now in the main it has "first", "second", and "third" and I get error C2664: cannot convert parameter 1 from 'const char[6]' to 'char'.
Thanks in advanced.
c++ Syntax (Toggle Plain Text)
#include <iostream> #include <cstring> using namespace std; class CharNode { public: CharNode(){} CharNode(char d, CharNode *lPtr) : data(d), linkPtr(lPtr){} CharNode *getLink() const {return linkPtr;} char getData() const {return data;} void setData(char d) {data=d;} void setLink(CharNode *lPtr) {linkPtr = lPtr;} void headInsert(CharNode* &head, char d); void deleteTail (CharNode *before); void printAll(); private: char data; CharNode *linkPtr; }; void CharNode::headInsert(CharNode* &head, char d) { head = new CharNode(d, head); } void CharNode::deleteTail(CharNode *head) { CharNode *discard, *tail; tail = head; // move tail to the last but one node while ((tail->getLink())->getLink() != NULL) tail = tail -> getLink(); tail -> setLink(NULL); discard = tail->getLink(); delete discard; } void CharNode::printAll() { if (this == NULL) cout << "Empty list\n"; else { int i = 1; CharNode *tempPtr = this; cout << "The linked list is: \n"; while (tempPtr != NULL) { cout << "Object " << i++ << ": " << tempPtr->getData() << endl; tempPtr = tempPtr->getLink(); } } } int main() { CharNode *head = new CharNode("First", NULL); head->headInsert(head,"Second"); head->headInsert(head,"Third"); head->printAll(); delete head; head=NULL; head->printAll(); system("PAUSE"); return 0; }
Last edited by Drifter666; Apr 1st, 2009 at 10:39 am.
Your CharNode contains member "char data;" and you're trying to assign a string "First" to that char. That isn't going to work because "First" is actually a pointer to a null terminated array of chars and you're trying to say: char = pointer to char.
If you want strings in your linked list you need to provide a different member type for the storage in the CharNode class.
This could be an STL string, a char* to memory you'll have to allocate yourself or, the simplest solution is a fixed length char array (if you do this make sure you copy all chars to it, don't just try and copy the pointer).
If you want strings in your linked list you need to provide a different member type for the storage in the CharNode class.
This could be an STL string, a char* to memory you'll have to allocate yourself or, the simplest solution is a fixed length char array (if you do this make sure you copy all chars to it, don't just try and copy the pointer).
•
•
Join Date: Apr 2009
Posts: 3
Reputation:
Solved Threads: 0
Thanks MrSpigot. Makes sense.
Ok. I made them pointers. The program runs but it only prints out the first letter of each string. So I tried to make the headinsert function like this:
head = new CharNode((strlen(d)+1), head);
but that doesn't work either.
I'm just looking for an easy way out right now. Let's say the fixed character length is 20. But when I try to implement I get tons more errors. I'm not familiar with linked lists at all, so this is all kinda new to me.
Again, thanks in advanced.
Ok. I made them pointers. The program runs but it only prints out the first letter of each string. So I tried to make the headinsert function like this:
head = new CharNode((strlen(d)+1), head);
but that doesn't work either.
I'm just looking for an easy way out right now. Let's say the fixed character length is 20. But when I try to implement I get tons more errors. I'm not familiar with linked lists at all, so this is all kinda new to me.
Again, thanks in advanced.
c++ Syntax (Toggle Plain Text)
#include <iostream> #include <cstring> using namespace std; class CharNode { public: CharNode(){} CharNode(char *d, CharNode *lPtr) : data(d), linkPtr(lPtr){} CharNode *getLink() const {return linkPtr;} char getData() const {return *data;} void setData(char *d) {data=d;} void setLink(CharNode *lPtr) {linkPtr = lPtr;} void headInsert(CharNode* &head, char *d); void deleteTail (CharNode *before); void printAll(); private: char *data; CharNode *linkPtr; }; void CharNode::headInsert(CharNode* &head, char *d) { head = new CharNode(d, head); } void CharNode::deleteTail(CharNode *head) { CharNode *discard, *tail; tail = head; // move tail to the last but one node while ((tail->getLink())->getLink() != NULL) tail = tail -> getLink(); tail -> setLink(NULL); discard = tail->getLink(); delete discard; } void CharNode::printAll() { if (this == NULL) cout << "Empty list\n"; else { int i = 1; CharNode *tempPtr = this; cout << "The linked list is: \n"; while (tempPtr != NULL) { cout << "Object " << i++ << ": " << tempPtr->getData() << endl; tempPtr = tempPtr->getLink(); } } } int main() { CharNode *head = new CharNode("First", NULL); head->headInsert(head,"Second"); head->headInsert(head,"Third"); head->printAll(); delete head; head=NULL; head->printAll(); system("PAUSE"); return 0; }
•
•
Join Date: Jul 2005
Posts: 1,761
Reputation:
Solved Threads: 283
C++ Syntax (Toggle Plain Text)
void CharNode::headInsert(CharNode* &head, char *d) { head = new CharNode(d, head); }
What the above says is create a new list every time you insert a new item. T'm almost positive that isn't what you want. I strongly recommend you have two classes, not one. One to define a node that will be inserted in a list, and the other to define the list itself. The node class will have one or more data member variables and one (or more if you want more than a singly linked list) pointer variable(s) to the node type itself. When you want to insert a new node into a list you declare memory for a new node, not a new list. Each list should have a node pointer to keep track of the first node in the list which is often called head.
Klatu Barada Nikto
•
•
Join Date: Apr 2009
Posts: 3
Reputation:
Solved Threads: 0
Thanks Lerner. I will take that approach next time.
I messed with the code again and with a little help from a local friend, we fixed it finally. Something I overlooked in the class definition. Thanks for your help guys.
here's the final code in case someone else needs to take a look.
I messed with the code again and with a little help from a local friend, we fixed it finally. Something I overlooked in the class definition. Thanks for your help guys.
here's the final code in case someone else needs to take a look.
c++ Syntax (Toggle Plain Text)
#include <iostream> #include <cstring> using namespace std; class CharNode { public: CharNode(){} CharNode(char d[], CharNode *lPtr) : linkPtr(lPtr) { strcpy(data,d); } CharNode *getLink() const {return linkPtr;} char *getData() {return data;} void setLink(CharNode *lPtr) {linkPtr = lPtr;} void headInsert(CharNode* &head, char *d); void deleteTail (CharNode *before); void printAll(); private: char data[20]; CharNode *linkPtr; }; void CharNode::headInsert(CharNode* &head, char *d) { head = new CharNode(d, head); } void CharNode::deleteTail(CharNode *head) { CharNode *discard, *tail; tail = head; // move tail to the last but one node while ((tail->getLink())->getLink() != NULL) tail = tail -> getLink(); tail -> setLink(NULL); discard = tail->getLink(); delete discard; } void CharNode::printAll() { if (this == NULL) cout << "Empty list\n"; else { int i = 1; CharNode *tempPtr = this; cout << "The list is: "; while (tempPtr != NULL) { cout << tempPtr->getData() << " "; tempPtr = tempPtr->getLink(); } } } int main() { CharNode *head = new CharNode("First", NULL); head->headInsert(head,"Second"); head->headInsert(head,"Third"); head->printAll(); cout << endl; delete head; head=NULL; head->printAll(); system("PAUSE"); return 0; }
![]() |
Similar Threads
- Dynamic char array - memory leak? (C)
- Linked List: Mp3 Menu (String and Delete problem) (C++)
- Linked List of Array (C++)
- traversing backwards thru double linked list (C++)
- Searching linked list (C)
- Linked list search problems (Computer Science)
- Seg Fault ~ Linked List Delete (C)
Other Threads in the C++ Forum
- Previous Thread: Browser Hack help
- Next Thread: How can I implement ISensLogon (COM)?
Views: 679 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






