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> ]
- 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.
@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..