gpt4 book ai didi

c++ - 错误: … was not declared in this scope

转载 作者:行者123 更新时间:2023-12-02 11:07:44 26 4
gpt4 key购买 nike

我正在使用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;
}

最佳答案

您将放在错误的位置:

更新

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;
}

关于c++ - 错误: … was not declared in this scope,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16684859/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com