c++ - Linked list with queue LNK2005 error -
c++ - Linked list with queue LNK2005 error -
hello wrote next programme (queue linkedlist) have error can not figure out how prepare happy if help me prepare it.
this code problem , 3 errors:
error 1 error lnk2005: "struct node * rear" (?rear@@3paunode@@a) defined in main.obj error 2 error lnk2005: "struct node * front" (?front@@3paunode@@a) defined in main.obj error 3 error lnk1169: 1 or more multiply defined symbols found
code -
queue.h
#ifndef _myqueue_h #define _myqueue_h #include <iostream> struct node { int data; node* next; } *rear, *front; void enqueue(int element); void dequeue(); #endif
main
#include <iostream> #include "myqueue.h" int main() { node *rear; node *front; enqueue(7); enqueue(4); enqueue(9); dequeue(); dequeue(); dequeue(); system("pause"); homecoming 0; }
queue.cpp
#include "myqueue.h" void enqueue(int element) { node* temp = new node; temp->data = element; temp->next = null; if (front == null) { front end = temp; } else { rear->next = temp; } rear = temp; } void dequeue() { node* temp = new node; if (front == null) { std::cout << "no elements" << std::endl; } else { temp = front; front end = front->next; std::cout << temp << std::endl; delete temp; } }
you have 2 declaration of rear
, front
pointer
one in queue.h , other in main
remove rear
, front
pointer main
int main() { //node *rear; //node *front; ..... }
have extern node *rear, *front;
in queue.h , define in queue.cpp
node *rear, *front;
c++ linked-list queue
Comments
Post a Comment