Expected unqualified id error in g ++

Hi guys, I need help creating this program for the class. We are working with g ++ (linux? Its via putty on the server) I have a lot of problems with this linked list. The current error it gives me is queue.cpp: 2: error: expected unqualified identifier before Γ’using I wonder if anyone can help me figure it out. A bit of searching shows that the problem seems to be in #define somewhere? the error shows that it is in the .cpp file, but I think it is in the .h file. Also, if you could give me any programming recommendations about anything that seems wrong, or if there is a better way to do this.

queue.h file below

#ifndef QUEUE_H #define QUEUE_H template <class Object> class Queue { public: Queue(); Queue(const Queue& a_queue); Queue& operator =(const Queue& rhs); bool enqueue(const Object& d); bool dequeue(const Object& d); bool isEmpty() const; ~Queue(); private: struct ListNode { Object obj; ListNode *next; }; ListNode *head; } #endif //Queue_H #include "queue.cpp" //include queue.cpp with file 

the queue.cpp file is here.

 #include <iostream> using namespace std; template <class Object> Queue<Object>::Queue() { head = NULL; } template <class Object> Queue<Object>::Queue(const Queue<Object>& a_queue) { head=NULL; *this=a_queue; } template <class Object> Queue<Object>& Queue<Object>::operator =(const Queue<Object> &rhs) { ListNode *nodePtr; nodePtr = rhs.head; if(this != rhs){ this.head = NULL; while(nodePtr != NULL){ this.enqueue(nodePtr->obj); nodePtr = nodePtr->next; } } } template <class Object> bool Queue<Object>::enqueue (const Object& d) //Enqueue { ListNode *newNode; ListNode *nodePtr; ListNode *previousNode; previousNode = NULL; nodePtr = NULL; newNode = new ListNode; newNode->obj = d; newNode->next = NULL; if(isEmpty){ head = newNode; return true; } else{ nodePtr = head; previousNode = NULL; while(nodePtr != NULL){ previousNode = nodePtr; nodePtr=nodePtr->next; } if(previousNode->next == NULL){ previousNode->next = newNode; return true; } else return false; } } template <class Object> bool Queue<Object>::dequeue (const Object& d) //Dequeue { ListNode *nodePtr; if(!head) return false; else{ if(head->next != NULL){ nodePtr = head; d=nodePtr->obj; head = head->next; delete nodePtr; }else delete head; return true; } } template <class Object> bool Queue<Object>::isEmpty() const //Check if Empty { if(!head) return true; else return false; } template <class Object> Queue<Object>::~Queue() //Destructor { Object temp; while (head != NULL) dequeue (temp); } 
+4
source share
2 answers
  • Do not include your implementation file from the header. Include the header from the implementation file.
  • I do not see that you say "using namespace std" in the header of your code, but you say in your comment that you are doing this. Not. Never say <<20> in the header file.
  • Put all template definitions in the header.
  • You are missing a semicolon in the class definition in the header.
  • The assignment operator is not safe. Make sure your copy constructor is correct, and then use the copy and swap icon.
  • Your copy constructor is incorrect. If you want to maintain a lazy copy (copy on write) then this is good, but you are missing the actual deep copy operation. Be careful with the copy constructor, because it is extremely important that you understand correctly.
+7
source

You will need a semicolon after the class declaration in the header.

 class Queue { public: Queue(); Queue(const Queue& a_queue); Queue& operator =(const Queue& rhs); bool enqueue(const Object& d); bool dequeue(const Object& d); bool isEmpty() const; ~Queue(); private: struct ListNode { Object obj; ListNode *next; }; ListNode *head; }; 
+1
source

Source: https://habr.com/ru/post/1347014/


All Articles