Calling a function inside switch case- Multiple Definition error

/*
* Program to implement Linkedlist.
*/

#include<cstdlib>
#include<iostream>



using namespace std;



class List{
private: //data

typedef struct node //struct enable to define two datas of diff types
{
int data; //number
node* next; //link
}* nodePtr;

//instead of using 'struct node* (C) or node* (C++)' you can use 'nodePtr'

nodePtr head;
nodePtr curr; //elements to manipulate linkedlist
nodePtr temp;

public: //functions

List()
{

head = NULL;
curr = NULL; //initializing head,curr,temp to NULL since list is empty
temp = NULL;


}//constructor has no datatype



void AddNode(int addData)
{
nodePtr n = new node;//creating pointer 'n' and point it to new node named
n -> next = NULL; //'node' added as last node in list
n -> data = addData; //data
//above code is node creation

if(head!=NULL) //if the list already has elements
{
curr = head;
while(curr->next != NULL) //traverse to last node
{
curr=curr->next;
}
curr->next = n; //add new node
}
else //list is empty
{
head=n;
}

}//no return



void DeleteNode(int delData)
{
nodePtr delPtr = NULL; //pointer for the purpose of deleting
temp = head; //used to patch with node after deleted node
curr = head; //points to data in node that matches deldata
while(curr != NULL && curr->data != delData) //traverse until finding
{ //deldata or NULL
temp=curr;
curr=curr->next;
}
if(curr == NULL)
{
cout<<delData<<" was not in the list\n";
delete delPtr; // hanging ptr may cause prblms during manipulation
}
else
{
delPtr = curr;
curr = curr->next; //patching the list : refer
temp->next=curr;
if(delPtr == head)
{
head= head->next; //if head is del node assign next as head
temp= NULL; //head will assign some random number if not
} //incremented before deleting
delete delPtr;
cout << "The Value "<<delData<<" was deleted.\n";
}
}//no return



void PrintList()
{
if(head==NULL)
{
cout<<"THE LIST IS EMPTY.\n";
}
else
{
cout<<"THE LIST CONTAINS:\n";
curr = head;
while(curr!=NULL)
{
cout<<curr->data<<endl;
curr=curr->next;
}
}
}//no return

};










int main()
{


char c;
cout<<"c-CONTINUE q-QUIT\n";
cin>>c;
if(c=='q'||c=='Q')
{
return 0;
}
else if(c=='c'||c=='C')
{
int c1;
do
{
cout<<"1. PRINT 2. ADD 3. DELETE 4. EXIT \n any key to exit\n";
cout<<"ENTER CHOICE:\n";


cin>>c1;


switch(c1)
{
case 1:
{



}

break;
case 2:
{



}
break;
case 3:
{



}
break;
case 4:cout<<"EXIT";
goto exit;
default:cout<<"INVALID\n";
break;
}


}while(c1!=0);
}
else
{
cout<<"INVALID";
}


exit: return 0;


}

My question is in the switch case blocks.
i tried creating object of class and access the functions in switch block it resulted in cross initialization of object or multiple function declaration.
Last edited on
I've removed all that unnecessary white space and added some code. You need to instantiate the object, then call methods on it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
 * Program to implement Linkedlist.
 */

#include<cstdlib>
#include<iostream>

using namespace std;

class List {
private: //data
  typedef struct node //struct enable to define two datas of diff types
  {
    int data; //number
    node* next; //link
  }* nodePtr;

  //instead of using 'struct node* (C) or node* (C++)' you can use 'nodePtr'
  nodePtr head;
  nodePtr curr; //elements to manipulate linkedlist
  nodePtr temp;

public: //functions
  List()
  {
    head = NULL;
    curr = NULL; //initializing head,curr,temp to NULL since list is empty
    temp = NULL;
  }//constructor has no datatype

  void AddNode(int addData)
  {
    nodePtr n = new node;//creating pointer 'n' and point it to new node named
    n -> next = NULL; //'node' added as last node in list
    n -> data = addData; //data
    //above code is node creation

    if(head!=NULL) //if the list already has elements
    {
      curr = head;
      while(curr->next != NULL) //traverse to last node
      {
        curr=curr->next;
      }
      curr->next = n; //add new node
    }
    else //list is empty
    {
      head=n;
    }
  }//no return

  void DeleteNode(int delData)
  {
    nodePtr delPtr = NULL; //pointer for the purpose of deleting
    temp = head; //used to patch with node after deleted node
    curr = head; //points to data in node that matches deldata
    while(curr != NULL && curr->data != delData) //traverse until finding
    { //deldata or NULL
      temp=curr;
      curr=curr->next;
    }
    if(curr == NULL)
    {
      cout<<delData<<" was not in the list\n";
      delete delPtr; // hanging ptr may cause prblms during manipulation
    }
    else
    {
      delPtr = curr;
      curr = curr->next; //patching the list : refer
      temp->next=curr;
      if(delPtr == head)
      {
        head= head->next; //if head is del node assign next as head
        temp= NULL; //head will assign some random number if not
      } //incremented before deleting
      delete delPtr;
      cout << "The Value "<<delData<<" was deleted.\n";
    }
  }//no return

  void PrintList()
  {
    if(head==NULL)
    {
      cout<<"THE LIST IS EMPTY.\n";
    }
    else
    {
      cout<<"THE LIST CONTAINS:\n";
      curr = head;
      while(curr!=NULL)
      {
      cout<<curr->data<<endl;
      curr=curr->next;
      }
    }
  }//no return
};

int main()
{
  char c;
  cout<<"c-CONTINUE q-QUIT\n";
  cin>>c;
  if(c=='q'||c=='Q')
  {
    return 0;
  }
  else if(c=='c'||c=='C')
  {
    List list; // kbw: instantiate object
    int c1;
    do
    {
      cout<<"1. PRINT 2. ADD 3. DELETE 4. EXIT \n any key to exit\n";
      cout<<"ENTER CHOICE:\n";
      cin>>c1;

      switch(c1)
      {
      case 1:
	list.PrintList(); // kbw: call the object's print method
        break;

      case 2:
	list.addData(c1); // kbw: call the object's add method
        break;

      case 3:
	list,deldata(c1); // kbw: call the object's delete method
        break;

      case 4:cout<<"EXIT";
        goto exit;

      default:cout<<"INVALID\n";
        break;
      }
    }while(c1!=0);
  }
  else
  {
    cout<<"INVALID";
  }

  exit: return 0;
}
/*
* Program to implement Linkedlist.
*/

#include<cstdlib>
#include<iostream>

using namespace std;

class List {
private: //data
typedef struct node //struct enable to define two datas of diff types
{
int data; //number
node* next; //link
}* nodePtr;

//instead of using 'struct node* (C) or node* (C++)' you can use 'nodePtr'
nodePtr head;
nodePtr curr; //elements to manipulate linkedlist
nodePtr temp;

public: //functions
List()
{
head = NULL;
curr = NULL; //initializing head,curr,temp to NULL since list is empty
temp = NULL;
}//constructor has no datatype

void AddNode(int addData)
{
nodePtr n = new node;//creating pointer 'n' and point it to new node named
n -> next = NULL; //'node' added as last node in list
n -> data = addData; //data
//above code is node creation

if(head!=NULL) //if the list already has elements
{
curr = head;
while(curr->next != NULL) //traverse to last node
{
curr=curr->next;
}
curr->next = n; //add new node
}
else //list is empty
{
head=n;
}
}//no return

void DeleteNode(int delData)
{
nodePtr delPtr = NULL; //pointer for the purpose of deleting
temp = head; //used to patch with node after deleted node
curr = head; //points to data in node that matches deldata
while(curr != NULL && curr->data != delData) //traverse until finding
{ //deldata or NULL
temp=curr;
curr=curr->next;
}
if(curr == NULL)
{
cout<<delData<<" was not in the list\n";
delete delPtr; // hanging ptr may cause prblms during manipulation
}
else
{
delPtr = curr;
curr = curr->next; //patching the list : refer
temp->next=curr;
if(delPtr == head)
{
head= head->next; //if head is del node assign next as head
temp= NULL; //head will assign some random number if not
} //incremented before deleting
delete delPtr;
cout << "The Value "<<delData<<" was deleted.\n";
}
}//no return

void PrintList()
{
if(head==NULL)
{
cout<<"THE LIST IS EMPTY.\n";
}
else
{
cout<<"THE LIST CONTAINS:\n";
curr = head;
while(curr!=NULL)
{
cout<<curr->data<<endl;
curr=curr->next;
}
}
}//no return
};

int main()
{
char c;
cout<<"c-CONTINUE q-QUIT\n";
cin>>c;
if(c=='q'||c=='Q')
{
return 0;
}
else if(c=='c'||c=='C')
{
List list; // kbw: instantiate object
int c1;
do
{
cout<<"1. PRINT 2. ADD 3. DELETE 4. EXIT \n any key to exit\n";
cout<<"ENTER CHOICE:\n";
cin>>c1;

switch(c1)
{
case 1:
list.PrintList(); // kbw: call the object's print method
break;

case 2:
cout<<"enter data: ";
int a;
cin>>a;
list.AddNode(a); // kbw: call the object's add method
break;

case 3:
cout<<"enter data: ";
int d;
cin>>d;
list.DeleteNode(d); // kbw: call the object's delete method
break;

case 4:cout<<"EXIT";
goto exit;

default:cout<<"INVALID\n";
break;
}
}while(c1!=0);
}
else
{
cout<<"INVALID";
}

exit: return 0;
}



Getting 'Multiple definition' error. dont know wats the problm :(
It would help if you formatted your code. There are tags in the palette on the right.

If the compiler reports an error, it tells you exactly what it's not happy with. When you edit that report and ask for help, you reduce the ability for anyone to help.
Last edited on
Your latest code compiles and runs for me.

For efficienct, AddNode should add the node to the front of the list. Otherwise creating a large list will take forever because each time you add a new element, the code must traverse the WHOLE list:
1
2
3
4
5
6
7
    void AddNode(int addData)
    {
        nodePtr n = new node;
        n->data = addData;
        n->next = head;
        head = n;
    }

Why are temp and curr members of the list class? There's no need to store them in between calls to the methods so they should be local variables.

You can greatly simplify DeleteNode. The trick is to keep a pointer to the pointer to the current node rather than a pointer to the current node or nullptr. The pointer-to-pointer will point to head, or the next pointer of the previous node:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    void DeleteNode(int delData)
    {
        for (nodePtr *headOrNext = &head;
             curr = *headOrNext;
             headOrNext = &curr->next) {

            if (curr->data == delData) {
                *headOrNext = curr->next;
                delete curr;
                cout << "The Value " << delData << " was deleted.\n";
                return;
            }
        }
        cout << delData << " was not in the list\n";
    }

Line 3 creates headOrNext, which is the pointer to pointer. It stars life pointing to head.
Line 4, curr = *headOrNext, is the test condition for the for loop. Notice that this is an assignment (=) rather than a comparison (==). It assigns curr to point to the current node. If the result is nullptr, the condition fails and the loop exits.
Line 5, headOrNext = &curr->next, sets headOrNext to point to curr's next pointer, thus setting up for another loop.

Line 8 is where headOrNext pays off. To unlink the current node, you simply say *headOrNext = curr->next. Since headOrNext points to whatever is pointing at curr, whether it's head or the previous node's next pointer, this line changes the pointer to point to the next item in the list (or nullptr is curr is the last node).
thanks ;-)
Topic archived. No new replies allowed.