C++ SGI STL中的绑定好友模板
我知道SGI STL和我一样老,但我仍然想弄清楚 在中,有一些代码如下所示:C++ SGI STL中的绑定好友模板,c++,stl,C++,Stl,我知道SGI STL和我一样老,但我仍然想弄清楚 在中,有一些代码如下所示: template <class T, class Sequence = Deque<T> > class Stack { friend bool operator== __STL_NULL_TMPL_ARGS (const Stack&, const Stack&); friend bool operator< __STL_NULL_TMPL_ARGS (c
template <class T, class Sequence = Deque<T> >
class Stack {
friend bool operator== __STL_NULL_TMPL_ARGS (const Stack&, const Stack&);
friend bool operator< __STL_NULL_TMPL_ARGS (const Stack&, const Stack&);
protected:
Sequence c;
};
template <class T, class Sequence>
bool operator== (const Stack<T, Sequence>& x, const Stack<T, Sequence>& y){
return x.c == y.c;
}
template <class T, class Sequence>
bool operator< (const Stack<T, Sequence>& x, const Stack<T, Sequence>& y){
return x.c < y.c;
}
# ifdef __STL_CLASS_PARTIAL_SPECIALIZATION
# define __STL_TEMPLATE_NULL template<>
# else
# define __STL_TEMPLATE_NULL
# endif
但当我试图用G++4.9.2编译它时,编译器说:
In file included from stack.cpp:1:0:
stack.h:13:22: error: declaration of ‘operator==’ as non-function
friend bool operator== __STL_NULL_TMPL_ARGS (const Stack&, const Stack&);
^
stack.h:13:22: error: expected ‘;’ at end of member declaration
In file included from iterator.h:3:0,
from deque.h:6,
from stack.h:5,
from stack.cpp:1:
stl_config.h:111:31: error: expected unqualified-id before ‘<’ token
# define __STL_NULL_TMPL_ARGS <>
^
stack.h:13:25: note: in expansion of macro ‘__STL_NULL_TMPL_ARGS’
friend bool operator== __STL_NULL_TMPL_ARGS (const Stack&, const Stack&);
堆栈中包含的文件中的。cpp:1:0:
stack.h:13:22:错误:将“operator==”声明为非函数
friend bool运算符==\uuuuSTL\uNULL\uTMPL\uargs(常量堆栈和,常量堆栈和);
^
stack.h:13:22:错误:应为“;”在成员声明末尾
在iterator.h:3:0中包含的文件中,
来自deque.h:6,
从堆栈h:5,
来自stack.cpp:1:
stl_config.h:111:31:错误:在'stl_stack.h
之前预期的非限定id有一个错误,该错误一定在当代编译器的雷达范围内。在将operator==
声明为模板之前提及operator==
是非法的
要解决此问题,请在定义堆栈之前声明运算符==
。但是如果你在那一点上声明它,你也可以定义它。并且声明将需要堆栈的前向声明
:
template <class T, class Sequence>
class stack; // Forward declare for sake of operator== declaration.
template <class T, class Sequence>
bool operator==(const stack<T, Sequence>& x, const stack<T, Sequence>& y) {
return x.c == y.c;
}
#ifndef __STL_LIMITED_DEFAULT_TEMPLATES
template <class T, class Sequence = deque<T> >
#else
…
模板
类堆栈;//为了运算符==声明,向前声明。
模板
布尔运算符==(常数堆栈&x,常数堆栈&y){
返回x.c==y.c;
}
#ifndef\uuuu STL\u LIMITED\u默认\u模板
模板
#否则
…
(当然,在顶部添加了操作符==
定义后,您将从底部删除它。)您可能正在查找\define\uu STL\u NULL\u TMPL\u ARGS
,第192行。代码可能不再一致,但如果问题更多地围绕友好模板的语法,您可以查看这里;我确信#define uu STL NULL TMPL ARGS
已经包含在内@Potatoswatter@Pyjamas我的意思是你复制粘贴了与此无关的\uu STL\u TEMPLATE\u NULL
的定义。@谢谢!我添加了一些转发声明,它成功了!