-1

我在 C++ 上做这个项目,我必须返回独特的元素。如果有人可以帮助我解决这个问题这是我整个代码的示例

#include <iostream.h>
class CService {
  private:
  string m_strSeller;
  //other stuff
};

class CServiceAnalizer {
 //other stuff
 unique function
};

这是我独特的功能的样子

CService unique (const CService& a, const CService& b) {
            if(a.m_strSeller==b.m_strSeller) {
                return b.m_strSeller;
            }

            CService result = a.m_strSeller;
            while (++a.m_strSeller != b.m_strSeller) {
                if (!(*result == *a.m_strSeller)) {
                    *(++result)=*a.m_strSeller;
                }
            }

            return ++result;
        }

主要错误之一是'm_strSeller' : cannot access private member declared in class 'CService'

如果有人可以向我解释究竟是什么让编译器出现这个错误,我该如何修复它们。

These are the errors from the compiler:
--------------------Configuration: Proekt - Win32 Debug--------------------
Compiling...
Proekt.cpp
D:\My Documents\OOP Proekt\Proekt\Proekt.cpp(191) : error C2248: 'm_strSeller' : cannot access private member declared in class 'CService'
        D:\My Documents\OOP Proekt\Proekt\Proekt.cpp(17) : see declaration of 'm_strSeller'
D:\My Documents\OOP Proekt\Proekt\Proekt.cpp(191) : error C2248: 'm_strSeller' : cannot access private member declared in class 'CService'
        D:\My Documents\OOP Proekt\Proekt\Proekt.cpp(17) : see declaration of 'm_strSeller'
D:\My Documents\OOP Proekt\Proekt\Proekt.cpp(192) : error C2248: 'm_strSeller' : cannot access private member declared in class 'CService'
        D:\My Documents\OOP Proekt\Proekt\Proekt.cpp(17) : see declaration of 'm_strSeller'
D:\My Documents\OOP Proekt\Proekt\Proekt.cpp(192) : error C2664: '__thiscall CService::CService(int)' : cannot convert parameter 1 from 'const class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'int'
        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
D:\My Documents\OOP Proekt\Proekt\Proekt.cpp(195) : error C2248: 'm_strSeller' : cannot access private member declared in class 'CService'
        D:\My Documents\OOP Proekt\Proekt\Proekt.cpp(17) : see declaration of 'm_strSeller'
D:\My Documents\OOP Proekt\Proekt\Proekt.cpp(195) : error C2440: 'initializing' : cannot convert from 'const class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'class CService'
        No constructor could take the source type, or constructor overload resolution was ambiguous
D:\My Documents\OOP Proekt\Proekt\Proekt.cpp(196) : error C2248: 'm_strSeller' : cannot access private member declared in class 'CService'
        D:\My Documents\OOP Proekt\Proekt\Proekt.cpp(17) : see declaration of 'm_strSeller'
D:\My Documents\OOP Proekt\Proekt\Proekt.cpp(196) : error C2675: unary '++' : 'const class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' does not define this operator or a conversion to a type acceptable to the p
redefined operator
D:\My Documents\OOP Proekt\Proekt\Proekt.cpp(196) : error C2248: 'm_strSeller' : cannot access private member declared in class 'CService'
        D:\My Documents\OOP Proekt\Proekt\Proekt.cpp(17) : see declaration of 'm_strSeller'
D:\My Documents\OOP Proekt\Proekt\Proekt.cpp(196) : fatal error C1903: unable to recover from previous error(s); stopping compilation
Error executing cl.exe.

Proekt.obj - 10 error(s), 0 warning(s)

我的整个代码:

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

    using namespace std;
    using std::ostream;
    using std::endl;
    using std::cout; 

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

    public:

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

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

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

        string GetClient() const
        {
            return m_strClient;
        }

        string GetSeller() const
        {
            return m_strSeller;
        }

        int GetMinutes() const
        {
            return m_iMinutes;
        }

        void SetClient (string c)
        {
            m_strClient = c;
        }
        void SetSeller (string s)
        {
            m_strSeller = s;
        }
        void SetMinutes (int m)
        {
            m_iMinutes = m;
        }

        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_iMinutes < obj.m_iMinutes;
        }

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

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

    };

    ostream& operator<<(ostream& os, const CService &obj) {
        os<<obj.GetClient()<<obj.GetSeller()<<obj.GetMinutes()<<endl;
        return os;
    }


    istream& operator>>(istream& is, CService &obj) {
        string tmp_strClient;
        string tmp_strSeller;
        int tmp_iMinutes;
        is>>tmp_strClient>>tmp_strSeller>>tmp_iMinutes;
        obj.SetClient(tmp_strClient);
        obj.SetSeller(tmp_strSeller);
        obj.SetMinutes(tmp_iMinutes);
        return is;
    }



    class CServiceAnalizer { 
    private:
        vector<CService>m_vData;

        void add(CService seller) {
            m_vData.push_back(seller);
        }
        //13.05.2013
    public:
        CService getSellerAt(int i)
        {
            return m_vData[i];
        }

        long getSellerCount()
        {
            return m_vData.size();
        }

        CServiceAnalizer()
        {
            ifstream fs;
            fs.open("test.txt");
            if(!fs.is_open()) cout<<"error opening file!\n";
            CService seller;
            while(!fs.eof())
            {
                fs>>seller;
                add(seller);
            }
        }

        CServiceAnalizer(const string& strFileName)
        {
            ifstream fs;
            fs.open(strFileName.c_str());
            if(!fs.is_open()) cout<<"error opening file!\n";
            CService seller;
            while(!fs.eof())
            {
                fs>>seller;
                add(seller);
            }
        }

        void Sort()
        {
            sort(m_vData.begin(),m_vData.end());
        }

        double calcMean() 
        {
            double sum=0;
            for (int i=0;i<m_vData.size();i++)
            {
                sum+=m_vData[i].GetMinutes();
            }
            return sum/m_vData.size();
        }

        vector<int> calcNums(int iR1,int iR2,int iR3,int iR4,int iR5)
        {
            vector<int> resultVector;
            for (int i=0;i<4;i++)
            {
                resultVector.push_back(0);
            }
            for (i=0;i<m_vData.size();i++)
            {
                if(m_vData[i].GetMinutes()>=iR1&&m_vData[i].GetMinutes()<iR2) resultVector[0]++;//[iR1-iR2)
                if(m_vData[i].GetMinutes()>=iR2&&m_vData[i].GetMinutes()<iR3) resultVector[1]++;//[iR2-iR3)
                if(m_vData[i].GetMinutes()>=iR3&&m_vData[i].GetMinutes()<iR4) resultVector[2]++;//[iR3-iR4)
                if(m_vData[i].GetMinutes()>=iR4&&m_vData[i].GetMinutes()<iR5) resultVector[3]++;//[iR4-iR5)
            }
            return resultVector;
        }

        CService unique (const CService& a, const CService& b) {
            if(a.m_strSeller==b.m_strSeller) {
                return b.m_strSeller;
            }

            CService result = a.m_strSeller;
            while (++a.m_strSeller != b.m_strSeller) {
                if (!(*result == *a.m_strSeller)) {
                    *(++result)=*a.m_strSeller;
                }
            }

            return ++result;
        }
    };

    ostream& operator<<(ostream& os, CServiceAnalizer &obj) 
    {
        for (int i=0;i<obj.getSellerCount();i++)
        {
            cout<<obj.getSellerAt(i);
        }
        return os;
    }

    int main() 
    {
        CServiceAnalizer myAnalyzer;
        //myAnalyzer.Sort();
        cout<<"Client"<<"Seller"<<"Minutes"<<endl;
        cout<<"----------------------------"<<endl;
        cout<<myAnalyzer;
        cout<<"CalcMean result:"<<myAnalyzer.calcMean()<<endl;
        vector<int>myCalcNums = myAnalyzer.calcNums(1,20,50,80,100);
        cout<<"CalcNums result:"<<myCalcNums[0]<<","<<myCalcNums[1]<<","<<myCalcNums[2]<<","<<myCalcNums[3]<<endl;
        system("pause");
        return 0;
    }
4

4 回答 4

1

您正在尝试访问m_strSeller私有的数据成员(例如,您无法访问它)。您应该实现 getter/setter 或CServiceAnalizer::unique()CService.

于 2013-05-15T20:16:12.263 回答
0

虽然 agetter会起作用,但它有暴露类内部结构的缺点。

另一种解决方案是添加一个unique方法CService(虽然我会给该方法另一个,更具表现力的名称)。

它可以是:

static CService CService::unique( const CService& a, const CService& b );

或者

CService CService::unique( const CService& other );

我真的不知道里面的逻辑应该做什么。您正在string非常有创意地使用该 ++。

编辑。

如果您尝试m_strSellerCService保存的对象中输出唯一的集合,m_vData则:

  • 添加CService::GetSeller方法
  • 添加一个std::set<std::string> CServiceAnalizer::GetUniqueSellers()遍历m_vData向量并将每个 iterm 的卖家添加到 std::set 的方法(这保证了唯一性)
于 2013-05-15T20:35:38.197 回答
0

我怀疑问题出在这样的行上

if(a.m_strSeller==b.m_strSeller) {

这是一个私有字段,因此只有该实例可以访问它。如果您需要阅读该字段,请使用吸气剂

public:
string get_m_strSeller()
{
    return this.m_strSeller;
}
于 2013-05-15T20:16:05.570 回答
0

==根据编辑更新更新==

其他人错过了其他明确的类型兼容性问题。

CService::m_strSeller 在您的标头中定义为字符串。它们不是指向字符串的指针,它们是字符串。这意味着几件事。

据我所知,您还没有定义一个接受字符串的构造函数或定义一个接受字符串作为 r 值的赋值运算符。

  • 字符串没有 ++ 运算符。

  • 你不能这样做

CService 结果 = a.m_strSeller

结果与字符串的类型不同,您只有 CService = CService 的赋值运算符。

  • 你也不能取消引用不是指针的东西。
    即*result、*a、*b和*a.m_strSellar或*b.m_strSellar。整个代码中没有指针,所以除非你是乘法,否则不应该有任何 *

  • 您还没有告诉我们 unique 正在尝试执行什么功能。

  • 您不能操纵常量引用。因此,除非您将 m_strSeller 定义为可变的(您没有也不应该),否则您的所有“++”操作在您的独特功能中都是非法的。

于 2013-05-15T20:29:37.543 回答