代码之家  ›  专栏  ›  技术社区  ›  Thomas Matthews

好友无法使用命名空间访问私有成员

  •  0
  • Thomas Matthews  · 技术社区  · 6 月前

    在使用MS Visual Studio 2019进行重载时,我遇到了以下错误 operator<< :

    Severity    Code    Description Project File    Line    Suppression State
    Error   C2248   'Instruction::Device_Impedance::m_minimum_ohms': cannot access private member declared in class 'Instruction::Device_Impedance' friend_ostream_namespace    device_impedance.cpp    13  
    Severity    Code    Description Project File    Line    Suppression State
    Error   C2248   'Instruction::Device_Impedance::m_maximum_ohms': cannot access private member declared in class 'Instruction::Device_Impedance' friend_ostream_namespace    device_impedance.cpp    14  
    

    设备阻抗hpp:

    #ifndef INSTRUCTION_DEVICE_IMPEDANCE_HPP
    #define INSTRUCTION_DEVICE_IMPEDANCE_HPP
    
    #include <string>
    #include <iostream>
    
    
    namespace Instruction
    {
    
    class Device_Impedance
    {
    public:
        Device_Impedance();
    
        Device_Impedance(const unsigned int min_ohms,
                         const unsigned int max_ohms);
           
        //! Constructor -- Copy
        Device_Impedance(const Device_Impedance&  di);
        
        //! Destructor
        ~Device_Impedance();
    
    public:
        Device_Impedance&
        operator=(const Device_Impedance& di);
    
        friend std::ostream& operator<< (std::ostream& out, const Instruction::Device_Impedance&   di);
       
    
        //--------------------------------------------------------------------------
        //  Public Methods
        //--------------------------------------------------------------------------
    public:
        const std::string&      get_name() const override;
        
        //--------------------------------------------------------------------------
        //  Private Members
        //--------------------------------------------------------------------------
    private:
        unsigned int    m_minimum_ohms;
        unsigned int    m_maximum_ohms;
    };
    
    
    }   // End namespace Instruction
    
    /*! @}  // End Doxygen Group
     */
    
    #endif // INSTRUCTION_DEVICE_IMPEDANCE_HPP  
    

    设备阻抗.cpp

    #include "device_impedance.hpp"
    
    using namespace Instruction;
    
    
    /*!
     *  \details    Output the instruction name to the stream.
     */
    std::ostream&
    operator<< (std::ostream& out, const Instruction::Device_Impedance&   di)
    {
            out << di.get_name()
    //***** The next two lines are causing the error.
                << "    " << di.m_minimum_ohms
                << ", "   << di.m_maximum_ohms
                << "\n";
    
        return out;
    }
    
    
    const std::string&
    Device_Impedance ::
    get_name() const
    {
        static std::string  instruction_name{"DEVICE_IMPEDANCE"};
        return instruction_name;
    }
    

    我是否有正确的命名空间语法来实现 操作员<&书信电报; ?

    2 回复  |  直到 6 月前
        1
  •  2
  •   user17732522    6 月前

    不,你的语法不正确。 using namespace 不会导致以下声明/定义禁止该命名空间。

    要么使用 namespace Instruction { /*definition of operator<< here*/ } 就像你在标题中做的那样,或者在定义中限定名称:

    std::ostream&
    Instruction::operator<< (std::ostream& out, const Instruction::Device_Impedance&   di)
    

    否则,您将定义全局 ::operator<< 超载,而不是 friend ed一个在封闭类的命名空间中禁止的类,即 Instruction .

    什么 使用命名空间 然而,这样做的目的是,您不必为了查找而限定名称,例如。 Instruction:: 函数中的参数是多余的。类中的声明也是如此。


    此外,正如问题评论中所指出的那样 override get_name 这也是错误的,因为这个函数实际上并不是一个覆盖。

        2
  •  0
  •   3CxEZiVlQ    6 月前
    namespace Instruction {
    
    class Device_Impedance {
     public:
      friend std::ostream& operator<<(std::ostream& out,
                                      const Instruction::Device_Impedance& di);
    
     private:
      unsigned int m_minimum_ohms;
      unsigned int m_maximum_ohms;
    };
    
    }  // namespace Instruction
    

    上述最小化类声明朋友 operator<< 在命名空间中 Instruction .

    using namespace Instruction;
    
    std::ostream& operator<<(std::ostream& out,
                             const Instruction::Device_Impedance& di) {
      out << di.get_name() << "    " << di.m_minimum_ohms << ", "
          << di.m_maximum_ohms << "\n";
      return out;
    }
    

    上述代码定义了 操作员<&书信电报; 在全局命名空间中,它 没有 定义朋友 操作员<&书信电报; 在命名空间中 说明书 .

    定义必须在命名空间中

    namespace Instruction {
    
    std::ostream& operator<<(std::ostream& out,
                             const Instruction::Device_Impedance& di) {
      out << di.get_name() << "    " << di.m_minimum_ohms << ", "
          << di.m_maximum_ohms << "\n";
      return out;
    }
    
    }  // namespace Instruction
    

    或者在课堂上

    namespace Instruction {
    
    class Device_Impedance {
     public:
      friend std::ostream& operator<<(std::ostream& out,
                                      const Instruction::Device_Impedance& di) {
        out << di.get_name() << "    " << di.m_minimum_ohms << ", "
            << di.m_maximum_ohms << "\n";
        return out;
      }
    
     private:
      unsigned int m_minimum_ohms;
      unsigned int m_maximum_ohms;
    };
    
    }  // namespace Instruction