-1

I'm experiencing a weird problem. I have the following class:

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

Implementation is very simple and for some reason it does not compile, claiming:

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

And it is referencing me to the last line. First, the member isn't even private, no members are actually private in this specific abstract class. I just can't spot the problem.

Here is the implementation of the constructor:

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

The other functions only use ifstream's built-in functions such as is_open and so. In the main program I initial an object that through his constructor is initializing many derived classes of RuleProvider and pushing them (as polymorphic pointers) into a vector. This is the code snippet in the constructor of that object:

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

Here are all of the functions declarations (I never mention the word RuleProvider in any of the functions' definition so I assume it's unnecessary):

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

And same exactly for another 2 classes, just using another name (and a different implementation of readNext()) - OrgRuleProvider and UserRuleProvider.

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

and here's the general object whose constructor is the above:

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

Where could the problem be? I suspect the basic RuleProvider's constructor for some reason.

Joseph
  • 31
  • 1
  • 6
  • 2
    Give us a minimal program that reproduces the error. This code is not enough. – David G Jan 25 '15 at 18:08
  • 1
    You should not start names of members with an underscore. That is reserved for the compiler builder. – wimh Jan 25 '15 at 18:14
  • @Wimmel What do you mean? Isn't it a convention? I use it all the time. – Joseph Jan 25 '15 at 18:15
  • 1
    @Wimmel: No, [that is not true](http://stackoverflow.com/a/228797/560648). – Lightness Races in Orbit Jan 25 '15 at 18:16
  • @Joseph: It's legal but it's not a very good convention. It's like "Hungarian notation lite". Try to avoid it if you can IMO; just give your members good names. – Lightness Races in Orbit Jan 25 '15 at 18:17
  • @LightnessRacesinOrbit So... There is no problem with it as long as it doesn't clash with global namespaces? How about the convention starting with 'm' as for 'member'? Anyway, that doesn't have anything to do with the problem, I guess (?) – Joseph Jan 25 '15 at 18:18
  • @Joseph: Yeah it's legal. It wouldn't be at global scope. Leading `m` is kind of ugly too IMO :) But no it has nothing to do with your problem. – Lightness Races in Orbit Jan 25 '15 at 18:20

1 Answers1

4

Problem is this ifstream _file;. Streams are not copyable.

Sadique
  • 22,572
  • 7
  • 65
  • 91
  • What do you mean? How do I solve it? If so, should I use a pointer to ifstream instead? – Joseph Jan 25 '15 at 18:07
  • How did that copy the file? – David G Jan 25 '15 at 18:07
  • Are you passing it without a reference is some function? – Sadique Jan 25 '15 at 18:12
  • @al-Acme no, I do not. – Joseph Jan 25 '15 at 18:13
  • @Joseph: Yes, you do. – Lightness Races in Orbit Jan 25 '15 at 18:15
  • @LightnessRacesinOrbit Where? Maybe without any purpose, could you explain it, please? – Joseph Jan 25 '15 at 18:20
  • 1
    @Joseph: How am I supposed to know?! I cannot see your code, and you did not post a complete, minimal testcase that reproduces the issue. I would guess you're copying a `RuleProvider` somewhere: attempting to do so will attempt to copy its members, too, and a stream cannot be copied (as it is a flow of data, not a container). If you read the _whole_ error message (instead of just a tiny part of it) you should see a handy line number in it..?! – Lightness Races in Orbit Jan 25 '15 at 18:20
  • @LightnessRacesinOrbit Would all the header files (all the function declarations within) suffice? – Joseph Jan 25 '15 at 18:22
  • 1
    @Joseph: No. Read http://kera.name/articles/2013/10/nobody-writes-testcases-any-more/ and http://stackoverflow.com/help/mcve, carefully. You should already have composed one in order to debug your problem! I don't understand how people can debug their programming problems without composing testcases, yet I see people come on here daily without one. It's weird. – Lightness Races in Orbit Jan 25 '15 at 18:23
  • @LightnessRacesinOrbit I can't debug it because it won't compile at all. I did read the post now, and basically the only places that COULD be related is when passing `RuleProvider` as an argument for a function. The only place was when it was a reference, not by content, and the word `ifstream` does not appear at all at my code besides this one. The line number is line 21, which is basically the line of the variable declaration, not very helpful. – Joseph Jan 25 '15 at 18:33
  • Well thanks anyway, I gave up to pointers. I'm still going to try and solve it afterwards. – Joseph Jan 25 '15 at 18:57
  • @Joseph: "Debugging" is a broad phrase meaning "find out what's going wrong". That includes analysing your source code to find out why it won't build! And, yes, passing the `RuleProvider` into a function _by value_ will cause this, because that involves a copy. – Lightness Races in Orbit Jan 25 '15 at 19:22