2013-05-06 98 views
0

您好我有這樣的ostream操作,讓我這個錯誤,當我編譯:cannot access private member declared in class 'CService'C++ ostream的操作問題

這裏是我的代碼:

friend ostream& operator <<(ostream& os, CService &obj); 

ostream& operator<<(ostream &os, CService &obj) { 

os<<obj.m_strClient; 
return os; 

} 

我試圖返回ostream&但不能解決問題,而是增加了一個錯誤syntax error : ';'

這裏是我的全部代碼:

#include <iostream> 
#include <string> 
#include <fstream> 
#include <vector> 
#include <algorithm> 

using namespace std; 

class CService { 
private: 
    string m_strClient; 
    string m_strSeller; 
    int m_iMinutes; 

public: 
    CService() { 

     m_strClient="N/A"; 
     m_strSeller="N/A"; 
     m_iMinutes=0; 
    } 

    CService(string c, string s, int m) { 

     m_strClient=c; 
     m_strSeller=s; 
     m_iMinutes=m; 

    } 

    CService(const CService &obj) { 

     m_strClient=obj.m_strClient; 
     m_strSeller=obj.m_strSeller; 
     m_iMinutes=obj.m_iMinutes; 

    } 

    string GetClient() { 

     return m_strClient; 

    } 

    string GetSeller() { 

     return m_strSeller; 

    } 

    int GetMusic() { 

     return m_iMinutes; 

    } 

    CService CService::operator =(CService obj) { 

     m_strClient=obj.m_strClient; 
     m_strSeller=obj.m_strSeller; 
     m_iMinutes=obj.m_iMinutes; 
     return *this; 

    } 

    bool operator < (const CService &obj) const { 

     return m_strSeller<obj.m_strSeller; 

    } 

    CService CService::operator +(const CService &obj) const { 

     return CService(m_iMinutes+obj.m_iMinutes); 

    } 

    friend ostream& operator <<(ostream& os, CService &obj); 



    bool CService::operator==(CService &obj) { 

     return (obj.m_strSeller==m_strSeller); 

    } 
}; 

ostream& operator<<(ostream &os, CService &obj) { 

    os<<obj.m_strClient; 
    return ostream&; 

} 
+0

你能提供一些背景?你有哪些「朋友」聲明? – 2013-05-06 18:01:44

+0

在類內部使用'ClassName :: MemberFunctionName'是錯誤的。在類中放入一個聲明('void foo();')並在類的外部執行'void ClassName :: foo(){}',或者直接在不使用範圍解析操作符的情況下將該定義放入類中。 – chris 2013-05-06 18:07:37

+0

如果這確實是你的整個代碼,它是一個頭,它應該包括守衛。 – chris 2013-05-06 18:12:39

回答

0

你有很多問題,主要問題是:

  • return os;而不是return ostream&;

  • 您還沒有構造函數接受int:添加CService(int m = 0)

  • 不要添加CService::在班級定義:刪除他們

我糾正他們(Live code)至少它會被編譯沒有錯誤:

class CService 
{ 
private: 
    string m_strClient; 
    string m_strSeller; 
    int m_iMinutes; 

public: 

    CService(int m = 0) 
    { 
     m_strClient = "N/A"; 
     m_strSeller = "N/A"; 
     m_iMinutes = m; 
    } 

    CService(string c, string s, int m) 
    { 
     m_strClient = c; 
     m_strSeller = s; 
     m_iMinutes = m; 
    } 

    CService(const CService &obj) 
    { 
     m_strClient = obj.m_strClient; 
     m_strSeller = obj.m_strSeller; 
     m_iMinutes = obj.m_iMinutes; 
    } 

    string GetClient() 
    { 
     return m_strClient; 
    } 

    string GetSeller() 
    { 
     return m_strSeller; 
    } 

    int GetMusic() 
    { 
     return m_iMinutes; 
    } 

    CService operator =(CService obj) 
    { 
     m_strClient = obj.m_strClient; 
     m_strSeller = obj.m_strSeller; 
     m_iMinutes = obj.m_iMinutes; 
     return *this; 
    } 

    bool operator<(const CService &obj) const 
    { 
     return m_strSeller < obj.m_strSeller; 
    } 

    CService operator +(const CService &obj) const 
    { 
     return CService(m_iMinutes + obj.m_iMinutes); 
    } 

    friend ostream& operator <<(ostream& os, CService &obj); 

    bool operator==(CService &obj) 
    { 
     return (obj.m_strSeller == m_strSeller); 
    } 
}; 

ostream& operator<<(ostream &os, CService &obj) 
{ 
    os << obj.m_strClient; 
    return os; 
} 
+0

我還有「無法訪問私人會員」錯誤 – solenoo 2013-05-06 18:21:13

+0

我添加了一個實時代碼,除非您正在做其他事情,否則這是不可能的。 – deepmax 2013-05-07 08:45:13

+0

我只是複製/粘貼您的代碼 – solenoo 2013-05-07 13:36:43