C ++ overload << error

I hope to get some help with the error I get - I was looking for similar questions that really did not give me what I needed. The following is a snippet of code:

 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 containing several lines - they may vary depending on other drop-down lists. I want to have a file in this window, as well as what the user selects from the drop-down lists that replenish him. Howvever, I get 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; missing body function

I am not sure why, because I believe that the operator overload syntax is fine - can anyone see something that I did stupidly or maybe missed - Many thanks for any help.

+4
source share
6 answers

Just define it outside the class definition or define it in a 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 }; 
+5
source

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

Instead, you need to implement operator << as a free function.

+3
source

You must define an overloaded operator<< outside the definition of NewSelectionDlg and scope CProductListBox respectively.

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

Also, should it be b , not o in <lt ;:

  std::cout << o.m_lstEclispeProducts; 
0
source

I went with my second decision.

 class NewSelectionDlg : public CDialog { // Construction public: class CProductListBox { public: friend ostream& operator <<(ostream& o, const CProductListBox& b) { return o << b.m_lstEclispeProducts; } }; 

I still get the error message - error C2039: "m_lstEclispeProducts": is not a member of "NewSelectionDlg :: CProductListBox"

I'm not sure why this happens because part of the NewSelectionDlg class contains this code (the corresponding line is in bold) - if you have additional help / suggestions, this will be a big 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;** 
0
source

When you use an ad:

friend void foo ();

what you do declares a function in the encompassing area of โ€‹โ€‹the namespace.

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

The same goes for operators.

0
source

Source: https://habr.com/ru/post/1339810/


All Articles