Below is my .h file and the code below that is my function that I'm having troubles with. Its suppose to take in a users topic and see if that topic exists, if it does exist then find the keyword, commentcompare will find where that keyword is and delete the comment. However its not deleting anything and its returning temp is NULL.
class comment //adds a comment
{
public:
comment(char * create_comment);
~comment();
int postcomment(char * add_comment);
int DisplayAll(); //display all comments
int Rate(int rate);
bool commentcompare(char * comm);
comment *next;
private:
char * a_comment;
int rating;
};
struct topicnode //checks to see if the topic is correct one
{
topicnode();
~topicnode();
int addcomment(char *add_comment);//add comment to the end of LLL
char *topicname;
comment * head;
topicnode *next;
};
class blog //class for the elements of blog entries and functions for it
{
public:
blog();
~blog();
int Post(char *topic, char *add_comment); //finds the post and adds comment
int Display(char *topic); //displays a particular thread
int Display(); //displays all
int rate(char *topic, char *keyword, int rating); //find topic and rate comment
int remove(char *topic, char *keyword); //remove a topic
int createtopic(char *topic); //create topic
private:
topicnode *head;
};
topicnode * current;
This means use the operator *. A common use of '*' is for multiplication. This means that current is never initialized and there for equates to NULL.
I'm also seeing this in other places, it is wise to usually put the '*' next to the variable. Have you also checked that topicnode::head is ever initialized? If it equals NULL then your code won't work and you'll receive a SEGFAULT message (Segmentation Fault). You need to make sure that topicnode::head is referencing valid memory before using it.
This means use the operator *. A common use of '*' is for multiplication. This means that current is never initialized and there for equates to NULL.
@snow:
??? In this context, the '*' operator is the operator used for declaring a pointer to an object. Also, current is initialized in the for loop on line 8. The position of the operator makes no difference, it is simply one of three operators based on its context (multiplication, dereference, or pointer declarator).
@OP:
Your for loop is incrementing 'temp' twice, meaning that you skip every second object. You should remove line 16, and probably break out of the loop when the topic has been found. You should also modify the prev object to make it point to the object after, otherwise it will be pointing to deallocated memory.
it is simply one of three operators based on its context (multiplication, dereference, or pointer declarator)
You're right, sorry about that. I don't know why I thought that it meant multiplication. My bad. That fact also would negate me saying that current is never initialized.