【发布时间】:2020-04-03 20:37:42
【问题描述】:
好吧,我正在尝试在我的第一个 C++ 堆栈上实现 copy_and_swap 习惯用法,因为我需要创建一个交换函数,并且这个交换函数需要是一个友元函数,我尝试通过这种方式来实现:
template <class T>
class Stack {
private:
int top;
T* a;
int MAX;
public:
Stack(int MAX);
Stack(Stack<T>& s);
bool push(T x);
bool pop();
T peek();
bool isEmpty();
friend void swap(Stack<T>& f, Stack<T>& s);
~Stack();
};
template <class T>
void Stack<T>::swap(Stack<T>& f, Stack<T>& s){
//I will put something where yet.
}
但是,VSCode 对交换功能这么说:类模型“Stack”没有“swap”成员(我翻译成英文,我的 VSCode 用葡萄牙语运行)。
如何在不收到此错误的情况下做到这一点?
【问题讨论】:
标签: c++ templates stack swap friend