当前位置: 首页 > 知识库问答 >
问题:

友元模板重载运算符

秦博延
2023-03-14

我对这个错误有意见

错误LNK2019未解析的外部符号"类std::basic_ostream

现在,< code>post所做的就是调用< code >操作符

宣言

namespace rob {     

template < typename T> class Stack {
    friend std::ostream& operator<< (std::ostream& os, const Stack<T>& a);
    void print(std::ostream& os, char ofc = ' ') const;
private:
    std::vector<T> arr;
};

定义

template < typename T>
inline std::ostream & rob::operator<<(std::ostream & os, const Stack<T>& a)                {     
    return a.print(os, ' ');
}
template<typename T>
inline void rob::Stack<T>::print(std::ostream & os, char c) const
{
    for (int i = 0; i != arr.size(); i++)
    {
        os << c << arr[i];
    }
    os << '\n';
}

它们分别位于. h文件和. hpp中,我要求运算符不是成员函数(用于赋值)。

共有3个答案

浦修文
2023-03-14

除了@LibertyPaul的答案之外,您还需要添加模板

namespace rob {

   template <typename T> class Stack {
        friend std::ostream& operator<< (std::ostream& os, const Stack<T>& a) {
              return a.arr.print(os, ' ');
        }
   };

}

韩阳飙
2023-03-14

您还应该在它实际所属的Rob命名空间中声明函数签名:

namespace rob {
template <typename T>
class Stack {
    friend std::ostream& operator<< (std::ostream& os, const Stack<T>& a);
};

template < typename T>
std::ostream& operator<< (std::ostream& os, const Stack<T>& a){
    ...
}
段干安和
2023-03-14

代码示例的问题;

template <typename T>
class Stack {
    friend std::ostream& operator<< (std::ostream& os, const Stack<T>& a);
    void print(std::ostream& os, char ofc = ' ') const;
    // ...
};

那是< code >运算符吗

std::ostream& operator<< (std::ostream& os, const Stack<int>& a) {/*...*/}

由于它没有实现,链接器无法找到它并导致您得到的错误。

作为旁注;gcc对此发出如下警告

< code >警告:友元声明' std::ostream

注意:(如果这不是您的意图,请确保函数模板已经声明并添加

这可能不是预期的,每个实例都有自己的实现。

要纠正这一点,您可以在<code>堆栈</code>类型之前声明模板运算符,然后将其声明为友元,即实例化。语法看起来有点笨拙,但实际上如下所示;

// forward declare the Stack
template <typename>
class Stack;

// forward declare the operator <<
template <typename T>
std::ostream& operator<<(std::ostream&, const Stack<T>&);

template <typename T>
class Stack {
    friend std::ostream& operator<< <>(std::ostream& os, const Stack<T>& a);
    // note the required <>        ^^^^
    void print(std::ostream& os, char ofc = ' ') const;
    // ...
};

template <typename T>
std::ostream& operator<<(std::ostream&, const Stack<T>&)
{
  // ... implement the operator
}

上述代码将运算符的友谊限制为堆栈的相应实例化,即运算符

备选方案包括允许友谊扩展到模板的所有实例;

template <typename T>
class Stack {
    template <typename T1>
    friend std::ostream& operator<<(std::ostream& os, const Stack<T1>& a);
    // ...
};

< code >运算符的实现

 类似资料:
  • 因此,我试图在模板类中包含输入和输出运算符的两个友元声明,但每次编译代码时,它似乎都无法识别该运算符。这是我的头文件。 错误:严重性代码描述项目文件行抑制状态错误LNK2019未解析外部符号“class std::basic_ostream” 这似乎是一个链接错误。 屏幕h 这是我的主要cpp文件 main.cpp 我注意到这个问题可以通过在类体中包含友元声明和函数定义来解决,但是我不想这样做。

  • 我在运算符过载时遇到问题 主要是我有 其中<code>Integer</code>只是<code>int</code>的包装,带有我需要的一些特殊功能。 然而,当我编译上面的代码时,我得到了错误C2679,它表示<code>binary' 我还试图删除友元声明中的参数,因此代码变成了: 但这会产生另一个错误:C2785:

  • 我有一个模板类包含其他类的优先级队列,我需要使用优先级重载器调用各个类重载器,根据各个类的偏好进行比较(在这种情况下是年龄,在另一个类中可能是价格。 我绝对相信我已经实现了不正确的运算符重载,因此非常感谢您的建议。 举个例子 我得到这个错误,我不知道我需要做什么来修复它。我必须将类重载保持为单个变量(Animal) 任务cpp:在“布尔运算符”的实例化中

  • 函数和整个类都可以声明为非模板类友元。使用类模板,可以声明各种各样的友元关系。友元可以在类模板与全局函数间、另一个类(可能是模板类)的成员函数间或整个类中(可能是模板类)建立。建立这种友元关系的符号可能很繁琐。 在下列X类的类模板中声明为: template<class T>class X 下列友元声明: friend void f1(); 使函数f1成为从上述类模板实例化的每个模板类的友元。 在

  • 我想知道如果函数的模板参数包括但不限于类的模板参数,如何使函数成为类的朋友并在类外定义函数。 例如,我有以下模板类和模板朋友函数: 如果我编译: 我会得到以下链接器错误:

  • 二元运算符可以重载为带有一个参数的非 static 成员函数,或者带有两个参数的非成员函数(参数之—必须是类的对象或者是对类的对象的引用)。 本章稍后要重载运算符 += ,当把它重载为带有一个参数的 String 类的非 static 成员函数时,如果 y 和 z 是 String 类的对象,则 y == z 将被处理为 y.operator+=(z) ,调用成员函数 operator+= ,声明