简体   繁体   English

C ++ Ostream运算符问题

[英]C++ ostream operator issue

Hi I have this ostream operator that gives me this error when I compile: cannot access private member declared in class 'CService' 嗨,我有这个ostream运算符,在编译时会出现此错误: cannot access private member declared in class 'CService'

here is my code: 这是我的代码:

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

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

os<<obj.m_strClient;
return os;

}

I tried returning ostream& but that doesn't fix the problem, instead it adds another error syntax error : ';' 我尝试返回ostream&但这不能解决问题,相反,它添加了另一个错误syntax error : ';'

Here is my entire code: 这是我的整个代码:

#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&;

}

You have many issues, main problems were: 您有很多问题,主要问题是:

  • return os; instead of return ostream&; 而不是return ostream&;

  • You haven't constructor which accepts int : add CService(int m = 0) 您没有接受int构造函数:add CService(int m = 0)

  • You shouldn't add CService:: with in class definition: remove them 您不应该在类定义中添加CService::和:删除它们

I corrected them ( Live code ) at least it will be compiled without errors: 我更正了它们( 实时代码 ),至少它将被编译而没有错误:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM