C++ 模板警告和错误帮助(gcc)
我正在处理一个容器类模板(用于int、bool、strings等),我一直被这个错误所困扰C++ 模板警告和错误帮助(gcc),c++,templates,gcc,warnings,C++,Templates,Gcc,Warnings,我正在处理一个容器类模板(用于int、bool、strings等),我一直被这个错误所困扰 cont.h:56: error: expected initializer before '&' token 本节 template <typename T> const Container & Container<T>::operator=(const Container<T> & rightCont){ 模板 const容器和容器::o
cont.h:56: error: expected initializer before '&' token
本节
template <typename T>
const Container & Container<T>::operator=(const Container<T> & rightCont){
模板
const容器和容器::operator=(const容器和rightCont){
我到底做错了什么
也不确定此警告消息的含义
cont.h:13: warning: friend declaration `bool operator==(const Container<T>&, const Container<T>&)' declares a non-template function
cont.h:13: warning: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here) -Wno-non-template-friend disables this warning
cont.h:13:警告:友元声明'bool operator==(const Container&,const Container&')声明了一个非模板函数
cont.h:13:警告:(如果这不是您想要的,请确保函数模板已经声明并添加到函数名称之后)-Wno non template friend禁用此警告
在这个位置上
template <typename T>
class Container{
friend bool operator==(const Container<T> &rhs,const Container<T> &lhs);
public:
模板
类容器{
friend bool运算符==(常量容器和rhs、常量容器和lhs);
公众:
在第一种情况下,您第一次说的只是容器
,而不是容器
在第二种情况下,我认为您需要重复模板
:虽然模板类的成员函数是隐式模板化的,但友元函数不一定是隐式模板化的,因此有必要更加明确。我不确定您需要什么样的确切语法,但我认为:
- 在类之前,
etctemplate bool operator==(
- 在类中,
etcbool操作符==(
我认为这就是错误消息试图传达的内容,尽管不是非常清楚。在第一种情况下,您第一次说的是
容器,而不是容器
在第二种情况下,我认为您需要重复模板
:虽然模板类的成员函数是隐式模板化的,但友元函数不一定是隐式模板化的,因此有必要更加明确。我不确定您需要什么样的确切语法,但我认为:
- 在类之前,
template bool operator==(
etc
- 在类中,
bool操作符==(
etc
我认为这就是错误消息试图传达的内容,尽管不是非常清楚。在第一种情况下,您是反向操作的。当您指定返回类型时,您必须将模板参数列表包含到模板标识符(容器
)中,但当您指定参数类型时,您不需要这样做(只需容器
就足够了)
将是一个朋友。但专业化的功能模板
template <class U>
bool operator==(const Container<U> &rhs, const Container<U> &lhs) {
// ...
}
如果你想成为上述模板所有专业化的朋友,你必须说
template <typename T>
class Container {
friend bool operator==<T>(const Container<T> &rhs, const Container<T> &lhs);
...
template <typename T>
class Container {
template <class U>
friend bool operator==(const Container<U> &rhs, const Container<U> &lhs);
...
模板
类容器{
模板
friend bool运算符==(常量容器和rhs、常量容器和lhs);
...
在第一种情况下,您是反向操作的。当您指定返回类型时,您必须将模板参数列表包含到模板标识符(容器
),但当您指定参数类型时,您不需要这样做(只需容器
就足够了)
将是一个朋友。但专业化的功能模板
template <class U>
bool operator==(const Container<U> &rhs, const Container<U> &lhs) {
// ...
}
如果你想成为上述模板所有专业化的朋友,你必须说
template <typename T>
class Container {
friend bool operator==<T>(const Container<T> &rhs, const Container<T> &lhs);
...
template <typename T>
class Container {
template <class U>
friend bool operator==(const Container<U> &rhs, const Container<U> &lhs);
...
模板
类容器{
模板
friend bool运算符==(常量容器和rhs、常量容器和lhs);
...