2014-10-16 3 views
1

연결 목록을 구현하는 대기열을 만들려고하지만 컴파일러 오류가 발생합니다. 오류는 소멸자 (모든 대문자 주석으로 표시)를 호출하는 행에서 오버로드 된 대입 연산자 함수에서 발생합니다. 나는 그것이 내 생성자/소멸자 선언의 구문과 관련이있는 간단한 수정 인 직감이있다.생성자 및 소멸자 선언 구문 템플릿 클래스

오류 것은 내가 국가에게 다음 코드를 얻고있다 : error C2512: 'Queue<char>::Queue' : no appropriate default constructor available

그것은 생성자를 언급,하지만 라인이가 의미하는 것은 내가 소멸자를 호출하려고 경우 아래의 하나입니다.

미리 도움을 주셔서 감사합니다.

#ifndef QUEUE_H 
#define QUEUE_H 
#include <iostream> 
using namespace std; 

template <class Type> 
class Queue  // Create a Queue data structure implementing a linked list 
{ 
    private:  // The private members 
     struct Cell  // The Cell class will be the blueprints for each link in the list 
     { 
      Type data;  // The information held by the cell 
      Cell* next;  // The link to the next cell 
     }; 

     Cell* first = NULL; 
     Cell* last = NULL; 


    public:  // The public members 
     Queue(Type); 
     bool isEmpty(); 
     void push(Type); 
     Type pop(); 
     Queue<Type>& operator=(Queue<Type>&); 
     friend ostream& operator<<(ostream&, const Queue<Type>&); 
     ~Queue(); 
}; 


template<class Type> 
Queue<Type>::Queue(Type inputData)  // Constructor that initializes the queue with a new cell that last and first point to 
{ 
    first = new Cell; 

    first->data = inputData; 
    first->next = NULL; 

    last = first; 
} 




template<class Type> 
Queue<Type>& Queue<Type>::operator=(Queue<Type>& queue)  // Overload "=" so that it performs a deep copy of a Queue object 
{ 
    if (!queue.isEmpty()) 
    { 
     ~Queue();  // HERE IS THE ERROR LINE 

     Cell* rhs = queue.first; 

     while (rhs != NULL) 
     { 
      push(rhs->data); 
      rhs = rhs->next; 
     } 
    } 

    return *this; 
} 




template<class Type> 
Queue<Type>::~Queue()  // Destructor that deallocates all of the memory used by the queue. 
{ 
    if (!isEmpty())  // We only need to deallocate the queue if it is non-empty 
    { 
     Cell *link = last; 

     while (link != NULL)  // Until we reach the end of the queue, keep deleting each link 
     { 
      pop(); 
     } 

     first = NULL; 
     last = NULL; 
    } 
    else  // If the queue is already empty, let the user know 
    { 
     cout << "Cannot call destructor. The list is already empty.\n"; 
    } 
} 
#endif 

답변

1

이 글타래를 체크하십시오 : Can i call destructor from its class method?. 이를 해결하는 쉬운 방법은 대기열을 비우는 함수를 작성한 다음 소멸자 및 대입 연산자에서 호출하는 것입니다.

template<class Type> 
void Queue<Type> empty(){ 
    if (!isEmpty())  // We only need to deallocate the queue if it is non-empty 
    { 
     Cell *link = last; 

     while (link != NULL)  // Until we reach the end of the queue, keep deleting each link 
     { 
      pop(); 
     } 

     first = NULL; 
     last = NULL; 
    } 
    else  // If the queue is already empty, let the user know 
    { 
     cout << "Cannot call empty. The list is already empty.\n"; 
    } 
} 

template<class Type> 
Queue<Type>& Queue<Type>::operator=(Queue<Type>& queue)  // Overload "=" so that it performs a deep copy of a Queue object 
{ 
    if (!queue.isEmpty()) 
    { 
     empty();  // Tada, no more error 

     Cell* rhs = queue.first; 

     while (rhs != NULL) 
     { 
      push(rhs->data); 
      rhs = rhs->next; 
     } 
    } 

    return *this; 
} 




template<class Type> 
Queue<Type>::~Queue()  // Deconstructor that deallocates all of the memory used by the queue. 
{ 
    empty(); 
} 
+0

이것은 도움이되었습니다, 감사합니다. 나는 지금 어떤 종류의 "해결되지 않은 외부 기호"로 오류를 얻고있다. 블레 – Peter

0

이것은 템플릿과 관련이 없습니다.

클래스에 대해 생성자를 선언하면 컴파일러에서 합성 된 기본 생성자 (즉, 인수가없는 생성자)가 삭제됩니다.

자신을 Queue()으로 정의해야합니다.

현재 글로벌 범위에서 지시문을 사용하는 것은 좋은 생각이 아닙니다.

0

난 당신이 매개 변수가없는 생성자를 정의해야합니다, 당신은이 작업을 수행 할 경우

Queue<char> quCh; 

처럼, 매개 변수없이 큐를 정의하는 것 같아요.

Queue(); 

또는이처럼 큐 정의해야합니다

Queue<char> quCh('a');