C++超载<<错误

发布于 2024-10-17 14:32:29 字数 806 浏览 3 评论 0 原文

我希望得到一些关于我遇到的错误的帮助 - 我搜索过类似的问题,但这些问题并没有真正给我我所追求的东西。下面列出了一个代码片段:

class NewSelectionDlg : public CDialog
{
// Construction
public:

  class CProductListBox
  {
    public:
    friend ostream& operator <<(ostream& o, const CProductListBox& b);
  };
   ostream& operator<<(ostream& o,  const CProductListBox& b)
  {
        std::cout << o.m_lstEclispeProducts;
        return o;
  }

我有一个包含许多字符串的列表框 - 这些字符串可能会根据所选的其他下拉框而有所不同。我想要将此框中的内容保存到文件中,以及用户从填充该文件的下拉列表中选择的内容。但是我收到以下错误(我正在 VS 2008 中开发)。

错误 C2804:二进制“运算符 <<” 参数过多
错误 C2333:'NewSelectionDlg::operator <<':函数声明中出现错误;跳过函数体

我不知道为什么,因为我相信重载运算符的语法是可以的 - 任何人都可以看到我所做的愚蠢或可能错过的任何事情 - 非常感谢您的帮助。

I am hoping to get some help with an error I am getting - I have searched similar questions which havent really gave me what I'm after. A code snippet is listed below:

class NewSelectionDlg : public CDialog
{
// Construction
public:

  class CProductListBox
  {
    public:
    friend ostream& operator <<(ostream& o, const CProductListBox& b);
  };
   ostream& operator<<(ostream& o,  const CProductListBox& b)
  {
        std::cout << o.m_lstEclispeProducts;
        return o;
  }

I have a list box that contains a number of strings - these can vary depending on other drop down boxes selected. I want to what is in this box to a file as well as what the user selects from the drop downs that popluate it. Howvever I am getting the following error (I am developing in VS 2008).

error C2804: binary 'operator <<' has too many parameters
error C2333: 'NewSelectionDlg::operator <<' : error in function declaration; skipping function body

I am not sure why as i belive the syntax of overloading the operator is OK - can anyone see anything I have done stupid or may have missed - Many Thanks for any Help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

等待我真够勒 2024-10-24 14:32:29

只需在类定义之外定义它或在声明友谊时在子类中定义它:

class NewSelectionDlg : public CDialog
{
// Construction
public:

  class CProductListBox
  {
    public:
    friend ostream& operator <<(ostream& o, const CProductListBox& b);
  };

// (...) Rest of NewSelectionDlg
}; 

ostream& operator <<(ostream& o, const NewSelectionDlg::CProductListBox& b)
{
    // Did you meant:
    return o << b.m_lstEclispeProducts;
} 

class NewSelectionDlg : public CDialog
{
// Construction
public:

  class CProductListBox
  {
    public:
    friend ostream& operator <<(ostream& o, const CProductListBox& b)
    {
        // Did you meant:
        return o << b.m_lstEclispeProducts;
    }
  };

// (...) Rest of NewSelectionDlg
}; 

Just define it outside of class definition or define it in subclass when declaring friendship:

class NewSelectionDlg : public CDialog
{
// Construction
public:

  class CProductListBox
  {
    public:
    friend ostream& operator <<(ostream& o, const CProductListBox& b);
  };

// (...) Rest of NewSelectionDlg
}; 

ostream& operator <<(ostream& o, const NewSelectionDlg::CProductListBox& b)
{
    // Did you meant:
    return o << b.m_lstEclispeProducts;
} 

or

class NewSelectionDlg : public CDialog
{
// Construction
public:

  class CProductListBox
  {
    public:
    friend ostream& operator <<(ostream& o, const CProductListBox& b)
    {
        // Did you meant:
        return o << b.m_lstEclispeProducts;
    }
  };

// (...) Rest of NewSelectionDlg
}; 
梦归所梦 2024-10-24 14:32:29

运算符 << 不能是成员函数。第一个参数必须是 std::ostream ;在您的代码中,第一个(隐式)参数是 this 指针,即 NewSelectionDlg* 类型的对象。

您需要将 operator << 实现为自由函数。

operator << must not be a member function. The first argument must be std::ostream; in your code, the first (implicit) argument is the this pointer, i.e. an object of type NewSelectionDlg*.

You need to implement operator << as a free function instead.

对你的占有欲 2024-10-24 14:32:29

您应该在 NewSelectionDlg 的定义和范围 CProductListBox 的定义之外相应地定义重载的运算符<<

ostream& operator<<(ostream& o, const NewSelectionDlg::CProductListBox& b)
{
    ...
}

You should define the overloaded operator<< outside the definition of NewSelectionDlg and scope CProductListBox accordingly.

ostream& operator<<(ostream& o, const NewSelectionDlg::CProductListBox& b)
{
    ...
}
[浮城] 2024-10-24 14:32:29

另外,在 << 中它应该是 b 而不是 o

    std::cout << o.m_lstEclispeProducts;

Also, should it be a b rather than an o in the <<:

    std::cout << o.m_lstEclispeProducts;
恬淡成诗 2024-10-24 14:32:29

我采用了你的第二个解决方案。

class NewSelectionDlg : public CDialog
{
// Construction
public:

  class CProductListBox
  {
    public:
    friend ostream& operator <<(ostream& o, const CProductListBox& b)
    {

       return o << b.m_lstEclispeProducts;
    }
  };

我仍然收到错误 - 错误 C2039: 'm_lstEclispeProducts' : 不是 'NewSelectionDlg::CProductListBox' 的成员

我不确定为什么这是因为 NewSelectionDlg 类的一部分包含此代码(相关行以粗体显示)-如果您有任何进一步的帮助/建议,这将是一个很大的帮助。谢谢

// Dialog Data

//{{AFX_DATA(NewSelectionDlg)

  enum { IDD = IDD_NEW_SELECTION };

  CButton   m_btnMessageBoard;

  CButton m_btnMoreInfo;

  CComboBox m_cmbOpenDocuments;

  CButton m_btnOk;

  CButton m_btnStateApprovals;

  CComboBox m_cmbProductType;

///  CListBox  m_lstSalesConcepts;

  CButton   m_chkObjectiveWizard;

  **CProductListBox  m_lstEclipseProducts;**

I went with your second solution.

class NewSelectionDlg : public CDialog
{
// Construction
public:

  class CProductListBox
  {
    public:
    friend ostream& operator <<(ostream& o, const CProductListBox& b)
    {

       return o << b.m_lstEclispeProducts;
    }
  };

I am still getting an error - error C2039: 'm_lstEclispeProducts' : is not a member of 'NewSelectionDlg::CProductListBox'

I am not sure as to why this is because part of the NewSelectionDlg class contains this code (relevant line in bold) - if you have any further help/suggestions it would be a great help. Thanks

// Dialog Data

//{{AFX_DATA(NewSelectionDlg)

  enum { IDD = IDD_NEW_SELECTION };

  CButton   m_btnMessageBoard;

  CButton m_btnMoreInfo;

  CComboBox m_cmbOpenDocuments;

  CButton m_btnOk;

  CButton m_btnStateApprovals;

  CComboBox m_cmbProductType;

///  CListBox  m_lstSalesConcepts;

  CButton   m_chkObjectiveWizard;

  **CProductListBox  m_lstEclipseProducts;**
心如荒岛 2024-10-24 14:32:29

当您使用声明时:

friend void foo();

您正在做的是在封闭的命名空间范围中声明一个函数。

namespace name {
   struct outer {
      struct inner {
         friend void foo(); // declares name::foo
      };
   };
   void foo() {} // defines it
}

对于运营商来说也是如此。

When you use the declaration:

friend void foo();

what you are doing is declaring a function in the enclosing namespace scope.

namespace name {
   struct outer {
      struct inner {
         friend void foo(); // declares name::foo
      };
   };
   void foo() {} // defines it
}

The same goes for operators.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文