繁体   English   中英

错误C2248:无法访问在类中声明的私有成员,编译器行为异常

[英]Error C2248: cannot access private member declared in class, compiler weird behavior

我遇到一个奇怪的问题。 我有以下课程:

#pragma once
#include <fstream>
#include "Rule.h"
#include <string>
#include <iostream>
using namespace std;
class RuleProvider
{
public:
    RuleProvider(string);
    bool isValid();
    string getError();
    bool isEOF();
    virtual Rule readNext() = 0;
    void set();
protected:
    string _error;
    string _path;
    ifstream _file;
};

实现非常简单,由于某种原因它无法编译,并声称:

error C2248: 'std::basic_ifstream<_Elem,_Traits>::basic_ifstream' : cannot access private member declared in class 'std::basic_ifstream<_Elem,_Traits>'

它使我引用了最后一行。 首先,成员甚至不是私有的,在这个特定的抽象类中,没有成员实际上是私有的。 我只是无法发现问题。

这是构造函数的实现:

RuleProvider::RuleProvider(string path) : _path(path)
{
    this->_file.open(path);
}

其他函数仅使用ifstream的内置函数,例如is_open等。 在主程序中,我初始化了一个对象,该对象通过他的构造函数正在初始化RuleProvider许多派生类,并将它们(作为多态指针)推送到向量中。 这是该对象的构造函数中的代码片段:

    (this->_providers).push_back(&this->_globalProvider);

for(int i = 0 ; i < orgProviderSize ; i++)
{
    (this->_providers).push_back(new OrgRuleProvider(orgProviderPath[i]));
}

for(int i = 0 ; i < userProviderSize ; i++)
{
    (this->_providers).push_back(new UserRuleProvider(userProviderPath[i]));
}

for(int i = 0 ; i < orgProviderSize + userProviderSize + 1 ; i++)
{
    while(!((this->_providers)[i]->isEOF()))
    {
        this->_rules.insert((this->_providers)[i]->readNext());
    }
}

这是所有函数的声明(我在任何函数的定义中都从未提到过RuleProvider一词,因此我认为这是不必要的):

class GlobalRuleProvider : public RuleProvider
{
public:
    GlobalRuleProvider(string);
    virtual Rule readNext();
    ~GlobalRuleProvider(void);
};

对于另外两个类,完全相同,只是使用另一个名称(以及readNext()的不同实现) OrgRuleProviderUserRuleProvider

class Rule
{
public:
    Rule(string, string, string, string, string);
    string getSrcIP() const;
    string getDstIP() const;
    string getSrcPort() const;
    string getDstPort() const;
    string getProtocol() const;
    bool operator==(const Rule& other) const;
    bool operator<(const Rule& other) const;
    bool operator>(const Rule& other) const;
private:
    static bool isValidIP(string);
    static bool isValidPort(string);
    static bool isValidProtocol(string);
    string _srcIP;
    string _srcPort;
    string _dstIP;
    string _dstPort;
    string _protocol;
};

这是上面构造函数的常规对象:

class PacketFilter
{
public:
    PacketFilter(string, string*, int, string*, int);
    bool filter(string srcIP, string srcPort, string dstIP, string dstPort, string protocol);
    ~PacketFilter(void);
private:
    void update();
    GlobalRuleProvider _globalProvider;
    vector<RuleProvider*> _providers;
    set<Rule> _rules;
};

问题可能在哪里? 我出于某种原因怀疑基本RuleProvider的构造函数。

问题是这个ifstream _file; 流不可复制。

暂无
暂无

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

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