【问题标题】:Can't initialize friend function operator <<无法初始化友元函数运算符 <<
【发布时间】:2011-11-04 03:08:32
【问题描述】:

我遇到了朋友功能的问题。

我认为这是代码中唯一需要的部分。我的问题在于这个函数。它说问题出在第一行,但我不知道这有多准确。

friend ostream & operator << (ostream & b, Book & a)
    {
    b.setf(ios::fixed | ios::showpoint);
    b.precision(2);
    b << "Title     :  \"" << a.title << "\"\n"
    << "Author      : \"" << a.author << "\"\n"
    << "Price       : $" << a.price / 100.0 << endl
    << "Genre       : " <<a.genre << endl
    << "In stock? " << (a.status ? "yes" : "no") << endl
    << endl;
    return b;
    }

我得到错误: lab10.cpp:95: 错误:无法初始化友元函数âoperator

lab10.cpp:95:错误:朋友声明不在类定义中

提前致谢

【问题讨论】:

    标签: c++ operator-overloading friend-function


    【解决方案1】:

    你必须指定函数是哪个类的朋友。您要么将该函数放在类声明中:

    class Book{
    ...
      friend ostream & operator << (ostream & b, Book & a)
        {
        b.setf(ios::fixed | ios::showpoint);
        b.precision(2);
        b << "Title     :  \"" << a.title << "\"\n"
        << "Author      : \"" << a.author << "\"\n"
        << "Price       : $" << a.price / 100.0 << endl
        << "Genre       : " <<a.genre << endl
        << "In stock? " << (a.status ? "yes" : "no") << endl
        << endl;
        return b;
      }
    };
    

    另一种方式是在类中声明为友元,并在其他地方定义:

    class Book{
    ...
        friend ostream & operator << (ostream & b, Book & a);
    };
    

    ...

    // Notice, there is no "friend" in definition!
    ostream & operator << (ostream & b, Book & a)
        {
        b.setf(ios::fixed | ios::showpoint);
        b.precision(2);
        b << "Title     :  \"" << a.title << "\"\n"
        << "Author      : \"" << a.author << "\"\n"
        << "Price       : $" << a.price / 100.0 << endl
        << "Genre       : " <<a.genre << endl
        << "In stock? " << (a.status ? "yes" : "no") << endl
        << endl;
        return b;
    }
    

    【讨论】:

    • 这解决了我的问题。非常感谢您的投入。朋友功能仍然让我感到困惑。
    • 请注意,类主体中定义的友元函数仅通过 ADL 可见。无论如何,这就是运算符的使用方式。
    【解决方案2】:

    你有在类中原型化的朋友函数吗?你需要在类中有一些东西表明这是一个友元函数。喜欢这条线

      friend ostream& operator<<(...);
    

    什么的。查找重载插入/提取运算符的完整示例以获取更多信息。

    【讨论】:

    • 我确实有它的原型,但感谢您的回复。我忘了把它放在那里。
    【解决方案3】:
    #include <iostream>
    #include <string>
    using namespace std;
    
    class Samp
    {
    public:
        int ID;
        string strName; 
        friend std::ostream& operator<<(std::ostream &os, const Samp& obj);
    };
     std::ostream& operator<<(std::ostream &os, const Samp& obj)
        {
            os << obj.ID<< “ ” << obj.strName;
            return os;
        }
    
    int main()
    {
       Samp obj, obj1;
        obj.ID = 100;
        obj.strName = "Hello";
        obj1=obj;
        cout << obj <<endl<< obj1;
    
    } 
    

    输出: 100 你好 100 你好 按任意键继续...

    这只能是友元函数,因为对象在

    【讨论】:

      猜你喜欢
      • 2023-01-02
      • 2010-12-20
      • 2013-05-14
      • 2013-07-27
      • 1970-01-01
      • 2013-03-30
      • 2021-07-03
      • 2011-03-19
      • 2017-07-26
      相关资源
      最近更新 更多