2013-05-22 7 views
-1

я работаю на очереди в C++ и у меня возникают некоторые проблемы с странными ошибками компиляции:ошибка: ... не была объявлена ​​в этой области

Queue.h:37:49: error: expected "," or "..." before "<" token
Queue.h: In copy constructor "Queue::Queue(Queue&)":
Queue.h:39:11: error: "other" was not declared in this scope
Queue.h: At global scope:
Queue.h:42:72: error: expected "," or "..." before "<" token
Queue.h: In member function "Queue& Queue::operator=(const Queue&)":
Queue.h:44:11: error: "other" was not declared in this scope

Может кто-нибудь помочь?

#if !defined QUEUE_SIZE 
#define QUEUE_SIZE 30 
#endif 

template <class TYPE> class Queue 
{ 
private: 
    TYPE *array; 
public: 
    Queue(Queue& other); 
    Queue(); 
    ~Queue(); 
    Queue& operator=(const Queue& other); 
    TYPE pushAndPop(TYPE x); 
}; 

template <class TYPE> Queue<TYPE>::Queue() 
{ 
    array=new TYPE[QUEUE_SIZE]; 
} 

template <class TYPE> Queue<TYPE>::~Queue() 
{ 
    delete [] array; 
} 

template <class TYPE> TYPE Queue<TYPE>::pushAndPop(TYPE other) 
{ 
    TYPE item = array[0]; 
    for(int x = 0; x<QUEUE_SIZE-1; x++){ 
    array[x]= array[x+1]; 
    } 
    array[QUEUE_SIZE-1] = other; 
    return item; 
} 

template <class TYPE> Queue<TYPE>:: Queue(Queue&<TYPE> other) 
{ 
    array = other.array; 
} 

template <class TYPE> Queue<TYPE>& Queue<TYPE>:: operator=(const Queue&<TYPE> other) 
{ 
    array = other->array; 
} 
+1

'Queue & ' -> 'Queue &' или просто оставить из '' . Вы должны взять 'const &' в copy-ctor, если вам не нужно использовать неконстантный '&'. Также возникает проблема с распределением памяти при использовании копии ctor (один и тот же «массив» будет удален несколько раз). – dyp

ответ

2

Вы положили & в неправильном месте:

обновление

template <class TYPE> Queue<TYPE>:: Queue(Queue&<TYPE> other) 
               ^^ 
{ 
    array = other.array; 
} 

template <class TYPE> Queue<TYPE>& Queue<TYPE>:: operator=(const Queue&<TYPE> other) 
                     ^^^ 
{ 
    array = other->array; // other is a reference, not pointer 
       ^^^ 
} 

в

template <class TYPE> Queue<TYPE>:: Queue(Queue<TYPE>& other) 
{ 
    //array = other.array; 
} 

template <class TYPE> Queue<TYPE>& Queue<TYPE>:: operator=(const Queue<TYPE>& other) 
{ 
    // check that it's not self-assign 
    // make sure you release current memory 
    // allocate new memory 
    // copy over content of array 
    // array = other.array; 
} 
1

Посмотрите на комментарии, чтобы увидеть изменения. Также, как указывали другие, меняют Queue&<TYPE> на Queue<TYPE>&.

template <class TYPE> class Queue 
{ 
private: 
    TYPE *array; 
public: 
    Queue(const Queue& other); // Take a const reference instead 
    Queue(); 
    ~Queue(); 
    Queue& operator=(const Queue& other); 
    TYPE pushAndPop(TYPE x); 
}; 

template <class TYPE> Queue<TYPE>::Queue() 
{ 
    array=new TYPE[QUEUE_SIZE]; 
} 

template <class TYPE> Queue<TYPE>::~Queue() 
{ 
    delete [] array; 
    array = NULL; // Be safe 
} 

template <class TYPE> TYPE Queue<TYPE>::pushAndPop(TYPE other) 
{ 
    TYPE item = array[0]; 
    for(int x = 0; x<QUEUE_SIZE-1; x++){ 
    array[x]= array[x+1]; 
    } 
    array[QUEUE_SIZE-1] = other; 
    return item; 
} 

template <class TYPE> Queue<TYPE>::Queue(const Queue<TYPE>& other) 
{ 
    array=new TYPE[QUEUE_SIZE]; 

    for(int x = 0; x<QUEUE_SIZE; x++){ 
    array[x]= other.array[x]; // Deep copy of array is required 
    } 
} 

template <class TYPE> Queue<TYPE>& Queue<TYPE>:: operator=(const Queue<TYPE>& other) 
{ 
    // this piece of code is repeated in copy-ctor, good candidate to extract as a method 
    for(int x = 0; x<QUEUE_SIZE; x++){ 
    array[x]= other.array[x]; // Deep copy of array is required, 
    } 

    return *this; 
} 
+0

Когда вызывается оператор присваивания, хранилище уже было выделено в одном из символов ctors и 'array' в действительную память. Либо «удалить» его перед назначением нового указателя на «массив», либо повторно использовать память. (Кстати, поэтому нужно использовать 'std :: unique_ptr' и' std :: shared_ptr';) Как указывал Биллз, проверьте самоопределение. – dyp

+0

Нет проблем. Не должны были размещать эти замечания один за другим ... (но не сразу увидели все проблемы). Может быть, вы могли бы очистить комментарии? – dyp

+0

Понимаю, читать это не так-то просто. Удаленные комментарии. – Arun

Смежные вопросы