inheritance

Hi! I have 2 classes where A inherits from B. I'm trying to use ofstream to create a logfile that both have the option to use but I'm not sure how to get the base class setup properly. This is basically what I have so far:

1
2
3
4
5
6
7
8
9
A.h
class A : public B
{
   public:
	A(string strFile);
	virtual ~A();
   protected:
        ofstream foutLOG;
};


1
2
3
4
5
6
A.cpp
A::A(string strFile) : foutLOG(strFile)
{
   open_log_file(strFile); //class B member function <---what to do ????
   //open_log_file(ofstream);   //cause of 'operator=' errors
}


I'm getting a series of errors that look like this:
>c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(604): error C2249: 'std::basic_ios<_Elem,_Traits>::operator =' : no accessible path to private member declared in virtual base 'std::basic_ios<_Elem,_Traits>'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\ios(177) : see declaration of 'std::basic_ios<_Elem,_Traits>::operator ='
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> This diagnostic occurred in the compiler generated function 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator =(const std::basic_ostream<_Elem,_Traits> &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]


There's nothing special about class B...

Thanks !!!
Last edited on
- Bad title, you don't have multiple inheritance in your code, and the errors messages don't refer to that
- Your snip does not relate to the error messages. Note that it complains about operator=
--- Your class `A' has a `CHarvester()' constructor
--- Your complain "I'm not sure how to get the base class setup properly", and you refuse to show the base class
- You leave out a lot of code so it is impossible to try to reproduce your error
- You leave out important part of the error messages (context)


> There's nothing special about class B...
Except that is non-copyable, non-assignable, because it has a non-copyable, non-assignable data member.

I don't want to read your mind.
@ne555
Sorry, I thought I had isolated the problem and tried to simplify things to save reading time. I updated the first post and hopefully this post will clear up what I'm trying to do. I just want both classes to print out to the same 'log.txt' file which class A determines.

Based on your comments I found the direct problem causing the 'operator=' errors (I attempted to pass the foutLOG object into class B). The program compiles now but the base class's logfile still doens't work. This is what the base class looks like:
1
2
3
4
5
6
7
8
9
10
11
12
13
//B.h
class B
{
public:
	B();
	virtual ~B();
	void open_log_file(string strFileName)	{ bLOG.open(strFileName, ios::app);	};
        //void open_log_file(ofstream log)             { bLOG = log; };   //'operator=' error

       void log_test()    { bLOG << "testing......"; };
protected:
       ofstream bLOG;
};


If I send in a different file name in A's constructor instead of strFile it will create the new logfile but nothing is printed in the log..
Last edited on
Use the inherited (base class) stream object in the derived class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <fstream>
#include <string>

struct A // base class, moveable, noncopyable
{
    virtual ~A() = default ;
    A() = default ; // file not opened
    A( std::string filename ) : log_file( filename, std::ios::app ) {}

    virtual bool open_log_file( std::string filename )
    {
        if( log_file.is_open() ) log_file.close() ;
        log_file.open( filename, std::ios::app ) ;
        return log_file.is_open() ;
    }

    virtual void test() { log_file << "from A: this is a test\n" ; }

    protected: std::ofstream log_file ; // protected; the derived class object can use this
};

struct B : A // derived class, moveable, noncopyable
{
    B() = default ; // file not opened
    B( std::string filename ) : A(filename) {}

    virtual void test() override
    {
        static int n = 0 ;
        // write to the stream inherited from the base class
        log_file << ++n << ". from B (override): this is a test\n" ;
    }
};

int main()
{
    for( int i=0 ; i < 5 ; ++i )
    {
        B b( "1.txt" ) ;
        A& a = b ;

        a.test() ;
        a.A::test() ;
        
        a.open_log_file( "2.txt" ) ;
        a.test() ;
        b.A::test() ;
        
        a.open_log_file( "1.txt" ) ;
        a.test() ;
    }

    std::cout << "1.txt contains\n----------------\n" << std::ifstream( "1.txt" ).rdbuf() ;
    std::cout << "\n\n2.txt contains\n---------------\n" << std::ifstream( "2.txt" ).rdbuf() ;
}

http://coliru.stacked-crooked.com/a/16d935f7c3b4f214
Thank you JLBorges ! I understand how to do it now.
Topic archived. No new replies allowed.