Why am I getting a Redefinition of a C++ class error? -
using following code below getting "redefinition of sortedll" error. removed "class sortedll" header file below. when remove sortedll header file can't declare sortedll in main.cpp file.
so stuck. missing?
sorted linked list header file
#ifndef sortedlinkedlist_sortedll_h #define sortedlinkedlist_sortedll_h struct node { int data; struct node * next; }; class sortedll { public: int find(int data); bool remove(int data); int size(); bool removeodd(); bool add(int data); private: struct node * phead; int length; }; #endif
sorted linked list cpp file
#include <iostream> #include "sortedll.h" class sortedll { private: // searches linked list linearly int findlinear(int data, struct node ** pprev, struct node ** pcurr){ // start @ head of list *pcurr = phead; *pprev = null; int index = 0; while (*pcurr != null){ if ((*pcurr)->data == data){ // found return index; } else { *pprev = *pcurr; *pcurr = (*pcurr)->next; index++; } } return -1; } bool findinsertpointlinear(struct node ** pprev, struct node ** pcurr, int data){ while (*pcurr != null){ // if current data less new data if ((*pcurr)->data < data){ // store previous node *pprev = *pcurr; // advance next position *pcurr = (*pcurr)->next; } // current value >= new val else { // found insertion point break; } } return true; } // returns value @ index or null if no value exists bool get(int index, int * val) { // check if out of bounds if (index < 0 || index > (length-1)) { // out of bounds return false; } struct node * pcurr = phead; (int i=0;i<=index;i++){ pcurr = pcurr->next; } if (val != null) { *val = pcurr->data; } return true; } public: bool add (int data) { struct node * pcurr = phead; struct node * pprev = null; bool returnval = true; // if list empty if (phead == null){ // add new node phead = (struct node *)malloc(sizeof(struct node)); phead->data = data; phead->next = null; // return success length++; return returnval; } // list not empty, find insertion point if (findinsertpointlinear(&pprev, &pcurr, data)){ // if inserting @ begininng of list if (pprev == null){ pprev = (struct node *)malloc(sizeof(struct node)); if (pprev != null) { pprev->data = data; pprev->next = pcurr; // keep track of head pointer phead = pprev; } else { // failed allocate memory returnval = false; } } // if inserting @ end of list else if (pcurr == null && pprev !=null) { pcurr = (struct node *)malloc(sizeof(struct node)); if (pcurr != null){ pcurr->data = data; pprev->next = pcurr; pcurr->next = null; } else { // failure allocate memory returnval = false; } } // if inserting in middle of list else { pprev->next = (struct node *) malloc (sizeof(struct node)); if (pprev->next != null){ pprev->next->data = data; pprev->next->next = pcurr; } else { // failure allocate memory returnval = false; } } } // increment size of list 1 if (returnval == true) length++; return returnval; } // returns index of first occurrence of data or -1 if not found int find(int data){ struct node * pprev = null; struct node * pcurr = null; return findlinear(data, &pprev, &pcurr); } bool remove(int data) { // try find data struct node * pprev = null; struct node * pcurr = null; int removeindex = findlinear(data, &pprev, &pcurr); bool returnval = false; // if found if (removeindex != -1){ // remove node return false; } // if removing @ head if (removeindex == 0){ if (pcurr != null){ phead = pcurr->next; free(pcurr); pcurr = null; returnval = true; } else { printf("trying remove element not exist\n"); } } // if removing @ end else if (removeindex == (length-1)){ if (pcurr != null) { free(pcurr); pcurr = null; if (pprev != null){ pprev->next = null; } returnval = true; } else { printf("trying remove element not exist\n"); } } // removing somewhere in middle else { if (pprev != null && pcurr != null){ struct node * temp = pcurr->next; free(pcurr); pcurr = null; pprev->next = temp; returnval = true; } // wrong else { printf("attempting remove element middle either previous or curr element null\n"); } } return returnval; } int size(){ return length; } bool removeodd(){ struct node * pcurr = phead; struct node * pprev = null; // iterate on list while (pcurr != null){ // if current data element odd if (pcurr->data %2 != 0){ // remove // removing @ head if (pcurr == phead){ phead = phead->next; free(pcurr); pcurr = phead; } // if removing @ end else if (pcurr->next == null){ free(pcurr); pcurr = null; pprev->next = null; } // removing somewhere in middle else { struct node * temp = pcurr->next; free(pcurr); pprev->next = temp; // advance pcurr next node pcurr = temp; } } // if current data element else { pprev = pcurr; pcurr = pcurr->next; } } return true; } }sortedll;
as @ paulmckenzie points out, you've defined class sortedll in both header , .cpp file. want in .cpp method definitions. like:
int sortedll ::findlinear(int data, struct node ** pprev, struct node ** pcurr) { ... }
Comments
Post a Comment