首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SGI STL中的绑定朋友模板

SGI STL中的绑定朋友模板
EN

Stack Overflow用户
提问于 2017-01-19 14:15:24
回答 1查看 80关注 0票数 1

我知道SGI STL和我自己一样老,但我还是想弄清楚。

stack.h中,有如下代码:

代码语言:javascript
复制
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;
}

config.h中,__STL_NULL_TMPL_ARGS的定义如下:

代码语言:javascript
复制
# ifdef __STL_CLASS_PARTIAL_SPECIALIZATION
#   define __STL_TEMPLATE_NULL template<>
# else
#   define __STL_TEMPLATE_NULL
# endif

但是,当我试图用G++ 4.9.2编译它时,编译器说:

代码语言:javascript
复制
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&);

我不知道为什么完全相同的代码不能在我的电脑上编译,这是现在的非法代码还是什么?

非常感谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-01-19 14:31:15

stl_stack.h有一个错误,这肯定是在当代编译器的关注之下。在将operator== <>声明为模板之前,提及operator==是非法的。

要解决这个问题,请在定义operator==之前声明stack。但是如果你在这一点上声明它,你也可以定义它。声明将需要stack的前向声明。

代码语言:javascript
复制
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
…

(当然,在将operator==定义添加到顶部之后,您将从底部删除它。)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41743957

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档