-
Classes
if i have one class called Message.cpp
and then another class called LinkedList.cpp
and another class called Node.cpp
and another class called FoundMessage.cpp
How do i link these together to do the following..
-> get messages from the Message.cpp class
-> put the message's info into a Node
-> The node is then inserted into the linkedlist
-> in FoundMessage.cpp there is a function that finds a particular message in the linked list.
assuming that i have all the code .. how would i go about this..
im not sure how classes relate to each other and if there is any restrictions on calling another class's functions..
any help?
-
so far im stuck with this happening
i have a node.h and a node.cpp
i also have linkedlist.cpp and linkedlist.h
in linkedlist.cpp im calling a function that is in node.cpp..
but i am getting this error
Code:
LinkedList.cpp:30: error: `makeNode' undeclared (first use this function)
??
in linkedlist.cpp i also have #include "linkedlist.h" and #include "node.h"
isnt this enough?
thanks
-
Post the code that gives the error.
-
just one quick note.. you might already know this.. but you might want to save your classes as .h header files... i.e. message.h
just be sure to include
Code:
#include "message.h"
along with your preprocessor directives (#include<whatever >) in your driver (.cpp) program. :cool:
then.. compile your .cpp program.. and the header file will automatically be compiled along with your .cpp program. :)
-
im just reviewing my code now and it looks like one biggg mess....
and im just falling into this huge pit of confusion..
hopefully help is on the way :D
ok heres my code
Code:
/* NODE.H */
class Node {
public:
Node();
~Node();
Node* makeNode(const char *s, int a, int b);
private:
string msg;
int s;
int t;
Node* nextPtr;
};
Code:
/* NODE.CPP */
#include "node.h"
Node * Node::makeNode(const char *s, int a, int b) {
Node * newNodePtr = (Node *)malloc(sizeof(Node));
if(newNodePtr == NULL) {
cerr << "Out of memory" << endl;
}
else {
newNodePtr->msg = s;
newNodePtr->t = a;
newNodePtr->s = b;
newNodePtr->nextPtr = NULL;
}
return newNodePtr;
}
Code:
/* LINKEDLIST.H */
#include "Node.h"
class LinkedList {
public:
LinkedList(LinkedList* listPtr); // constructor
void Insert(LinkedList* listPtr);
void insertItem(LinkedList* listPtr, const char * s, int type, int status);
int deleteNodes(LinkedList* listPtr);
private:
int count;
Node* headPtr;
};
hopefully you can see from this, im TRYING to implement it so that i have a linked list of nodes.. not sure if that private member Node* headPtr should be LinkedList * headPtr or not...
i want to be able to do things like..
Code:
Node * newNode;
newNode->headPtr->msg = "hello";
so im not sure if those declarations and the structure are correct..
and finally the linkedlist.cpp where makeNode error (and many others) was occuring..
Code:
/* LINKEDLIST.CPP */
#include "LinkedList.h"
#include "Node.h"
void LinkedList::insertItem(LinkedList* listPtr, const char * item, int type, int status) {
Node* newNodePtr = makeNode(item, type, status);
Node* prevPtr = NULL;
if (position == 0)
{
newNodePtr->nextPtr = listPtr->headPtr;
listPtr->headPtr = newNodePtr;
}
else
{
prevPtr = setPosition(listPtr, position-1);
newNodePtr->nextPtr = prevPtr->nextPtr;
prevPtr->nextPtr = newNodePtr;
}
listPtr->count++;
}
i know thats heaps of code to take in.. hoep you can help
THANKS :D