Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 732
user617702 Avatar asked Jan 21 '23 11:01

user617702


1 Answers

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
}; 
like image 109
Pawel Zubrycki Avatar answered Jan 30 '23 06:01

Pawel Zubrycki