开发者

C++ overloading << error

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开发者_StackOverflow the operator is OK - can anyone see anything I have done stupid or may have missed - Many Thanks for any Help.


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
}; 


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.


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

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


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

    std::cout << o.m_lstEclispeProducts;


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;**


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜