Hi,
I have some code from an example, that I want to retrofit into my
project. The code from the example has the following line:
SharedAppenderP tr myAppender( new RollingFileAppe nder("MyAppende r"))
I want to move this line of code to a constructor in a wrapper class, so
that I can determine the appropriate Appender name during the object's
construction.
I have done the ff:
declared a privariate variable in my wrapper class as ff:
SharedAppenderP tr m_Appender ;
In my (wrapper class) constructor, I have the following code:
.....
m_Appender = new RollingFileAppe nder(log_file_n ame)
.....
However, I am getting linking errors (unresolved external symbol). I was
obviously wrong in thinking that I could use copy assignment in this
way. This is the actual liker error I'm getting (Although I am using MS
tools in this instance, I don't believe its a MS specific problem).
Auditor error LNK2019: unresolved external symbol "__declspec(dll import)
public: class log4cplus::help ers::SharedObje ctPtr<class
log4cplus::Appe nder> & __thiscall
log4cplus::help ers::SharedObje ctPtr<class
log4cplus::Appe nder>::operator =(class log4cplus::Appe nder *)"
(__imp_??4?$Sha redObjectPtr@VA ppender@log4cpl us@@@helpers@lo g4cplus@@QAEAAV 012@PAVAppender @2@@Z)
referenced in function "public: __thiscall Auditor::Audito r(int,int)"
(??0Auditor@@QA E@HH@Z)
Any helpful suggestions (or better still, an actual solution to the
problem) will be greatly appreciated.
MTIA
Aug 11 '05
15 3360
* Alfonso Morra: Here is the complete code of an example that I am using to write my wrapper class:
int main( ) { SharedAppenderP tr myAppender( new RollingFileAppe nder("myLogFile .log")) ; myAppender->setName("myApp enderName") ; std::auto_ptr<L ayout> myLayout = std::auto_ptr<L ayout>(new log4cplus::Patt ernLayout("%d{% Y-%m-%d %a %H:%M:%S} %-5p [%t]: %m%n")); myAppender->setLayout( myLayout ); Logger myLogger= Logger::getInst ance("myLoggerN ame") ; myLogger.addApp ender(myAppende r); myLogger.setLog Level ( INFO_LOG_LEVEL ); myLogger.log( INFO_LOG_LEVEL, "Info Level Message") ; myLogger.log( TRACE_LOG_LEVEL , "Trace Level Mesage") ; }
As you can see, the variables are all local variables that go out of scope after main has ceased. What I am doing is wrapping up the functionality provided by main, into a wrapper class (lets call this LogWriter). It is clear from the code above, that I will need variables (or pointers to objects) of type SharedAppenderP tr, Layout, LogLevel and Logger at the very least. So armed with these facts, I proced as follows:
class LogWriter { public: LogWriter(int,L ogLevel); virtual ~LogWriter() ;
LogMsg(const string&) ; setLogLevel(Log Level) ; ...
private: /* Keep hidden from prying eyes*/ LogWriter(const LogWriter&); LogWriter& operator=(const LogWriter&);
Presumably this assignment operator is not implemented, and if so, good.
Otherwise, if it's implemented it won't fly if SharedAppenderP tr doesn't
provide an assignment operator.
But it's possible that you just haven't #included the source for that
operator implementation, or not linked with the appropriate object file or
library.
/* Variables */ SharedAppenderP tr m_Appender ; //Default ctor invoked log4cplus::tstr ing m_AppenderName ; log4cplus::tstr ing m_LoggerName ; log4cplus::tstr ing m_logFilename ; std::auto_ptr<L ayout> m_Layout ; //Default ctor invoked LogLevel m_lvl ; Logger m_Logger ; //Default ctor invoked bool m_debugOn ;
/*Methods */ log4cplus::tstr ing getLogNameByMod uleId( int ) ;
};
Since we have m_Appender, m_Layout and m_Logger already constructed (default ctors),
Here's the thing: you do not have them already constructed.
If you don't initialize them in a constructor initializer list they'll be
default constructed, and that's what you're currently experiencing.
The way to avoid that is to use a constructor initializer list. ;-)
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Alf P. Steinbach wrote: * Alfonso Morra:
Here is the complete code of an example that I am using to write my wrapper class:
int main( ) { SharedAppenderP tr myAppender( new RollingFileAppe nder("myLogFile .log")) ; myAppender->setName("myApp enderName") ; std::auto_ptr<L ayout> myLayout = std::auto_ptr<L ayout>(new log4cplus::Pa tternLayout("%d {%Y-%m-%d %a %H:%M:%S} %-5p [%t]: %m%n")); myAppender->setLayout( myLayout ); Logger myLogger= Logger::getInst ance("myLoggerN ame") ; myLogger.addApp ender(myAppende r); myLogger.setLog Level ( INFO_LOG_LEVEL ); myLogger.log( INFO_LOG_LEVEL, "Info Level Message") ; myLogger.log( TRACE_LOG_LEVEL , "Trace Level Mesage") ; }
As you can see, the variables are all local variables that go out of scope after main has ceased. What I am doing is wrapping up the functionali ty provided by main, into a wrapper class (lets call this LogWriter). It is clear from the code above, that I will need variables (or pointers to objects) of type SharedAppenderP tr, Layout, LogLevel and Logger at the very least. So armed with these facts, I proced as follows:
class LogWriter { public: LogWriter(int,L ogLevel); virtual ~LogWriter() ;
LogMsg(const string&) ; setLogLevel(Log Level) ; ...
private: /* Keep hidden from prying eyes*/ LogWriter(const LogWriter&); LogWriter& operator=(const LogWriter&);
Presumably this assignment operator is not implemented, and if so, good.
Otherwise, if it's implemented it won't fly if SharedAppenderP tr doesn't provide an assignment operator.
But it's possible that you just haven't #included the source for that operator implementation, or not linked with the appropriate object file or library. /* Variables */ SharedAppenderP tr m_Appender ; //Default ctor invoked log4cplus::tstr ing m_AppenderName ; log4cplus::tstr ing m_LoggerName ; log4cplus::tstr ing m_logFilename ; std::auto_ptr<L ayout> m_Layout ; //Default ctor invoked LogLevel m_lvl ; Logger m_Logger ; //Default ctor invoked bool m_debugOn ;
/*Methods */ log4cplus::tstr ing getLogNameByMod uleId( int ) ;
};
Since we have m_Appender, m_Layout and m_Logger already constructed (default ctors),
Here's the thing: you do not have them already constructed.
If you don't initialize them in a constructor initializer list they'll be default constructed, and that's what you're currently experiencing.
The way to avoid that is to use a constructor initializer list. ;-)
Ok, two things spring to mind ... my first guess was that the assignment
operator was not implemented but I discounte this straight away because
of the "rule of three". After all, the authors of the library are
experienced C++ programmers - plus because it is open sourced, someone
would have spotted such an oversight straight away.
As you will see from my code comments, I pointted that the variables
will be constructed via their "default ctors". The point which (you
still seem to be missing) is taht there is valuable information passed
to the LogWriter ctor - and I do not have this information at the point
of the declaration/definition of the private variables. So the question
must remain, How do I create (for example), an instance of a
SharedAppender pointer, using the pointer returned by the expression "
new RollingFileAppe nder("myLogFile .log")" ?
This is what I have been trying to find out all along. thanks
* Alfonso Morra: The point which (you still seem to be missing) is taht there is valuable information passed to the LogWriter ctor - and I do not have this information at the point of the declaration/definition of the private variables.
As you write, you have it when your LogWriter constructor is called.
So the question must remain, How do I create (for example), an instance of a SharedAppender pointer, using the pointer returned by the expression " new RollingFileAppe nder("myLogFile .log")" ?
Use a constructor initializer list.
This is what I have been trying to find out all along. thanks
Why don't you just try it (and, why haven't you)?
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
"Alfonso Morra" <sw***********@ the-ring.com> wrote in message
news:dd******** **@nwrdmz03.dmz .ncs.ea.ibs-infra.bt.com...
Here's the thing: you do not have them already constructed.
If you don't initialize them in a constructor initializer list they'll be default constructed, and that's what you're currently experiencing.
The way to avoid that is to use a constructor initializer list. ;-)
Ok, two things spring to mind ... my first guess was that the assignment operator was not implemented but I discounte this straight away because of the "rule of three". After all, the authors of the library are experienced C++ programmers - plus because it is open sourced, someone would have spotted such an oversight straight away.
As you will see from my code comments, I pointted that the variables will be constructed via their "default ctors". The point which (you still seem to be missing) is taht there is valuable information passed to the LogWriter ctor - and I do not have this information at the point of the declaration/definition of the private variables. So the question must remain, How do I create (for example), an instance of a SharedAppender pointer, using the pointer returned by the expression " new RollingFileAppe nder("myLogFile .log")" ?
This is what I have been trying to find out all along. thanks
Having the declaration of an object in your class does _not_ "default
construct" the object. Member objects are constructed at run-time, during
the construction of the object containing them. And you can control that
construction by adding them to an initialization list in your LogWriter
constructor. They are only default constructed if you _don't_ put them in
the initializer list. (And even the, it's during the run-time construction
of an instance of the LogWriter object.) You might want to read up on
initializer lists, especially how they are used for member object and base
class object construction.
Also, if for some reason you _do_ need to create members _after_
construction of the containing object, then make them member pointers, not
member objects, and add appropriate new and delete calls.
One more thing: notice that m_Appender appears to be a pointer. When the
LogWriter object is constructed, no construction takes place of any object
for it unless you write the code to do so (via a call to new), despite your
code comment that it's default constructed at that point.
-Howard
Howard wrote: "Alfonso Morra" <sw***********@ the-ring.com> wrote in message news:dd******** **@nwrdmz03.dmz .ncs.ea.ibs-infra.bt.com...
Here's the thing: you do not have them already constructed.
If you don't initialize them in a constructor initializer list they'll be default constructed, and that's what you're currently experiencing.
The way to avoid that is to use a constructor initializer list. ;-)
Ok, two things spring to mind ... my first guess was that the assignment operator was not implemented but I discounte this straight away because of the "rule of three". After all, the authors of the library are experienced C++ programmers - plus because it is open sourced, someone would have spotted such an oversight straight away.
As you will see from my code comments, I pointted that the variables will be constructed via their "default ctors". The point which (you still seem to be missing) is taht there is valuable information passed to the LogWriter ctor - and I do not have this information at the point of the declaration/definition of the private variables. So the question must remain, How do I create (for example), an instance of a SharedAppender pointer, using the pointer returned by the expression " new RollingFileAp pender("myLogFi le.log")" ?
This is what I have been trying to find out all along. thanks
Having the declaration of an object in your class does _not_ "default construct" the object. Member objects are constructed at run-time, during the construction of the object containing them. And you can control that construction by adding them to an initialization list in your LogWriter constructor. They are only default constructed if you _don't_ put them in the initializer list. (And even the, it's during the run-time construction of an instance of the LogWriter object.) You might want to read up on initializer lists, especially how they are used for member object and base class object construction.
Also, if for some reason you _do_ need to create members _after_ construction of the containing object, then make them member pointers, not member objects, and add appropriate new and delete calls.
One more thing: notice that m_Appender appears to be a pointer. When the LogWriter object is constructed, no construction takes place of any object for it unless you write the code to do so (via a call to new), despite your code comment that it's default constructed at that point.
-Howard
For some reason, its all begining to make sense to me now. Although you
are saying pretty much what Alf said earlier, possibly, it was your more
verbose explanation that rammed the message home. Many thanks to both of
you. I believe I have enough insight now to tackle the problem. Thanks
Howard wrote: "Alfonso Morra" <sw***********@ the-ring.com> wrote in message news:dd******** **@nwrdmz03.dmz .ncs.ea.ibs-infra.bt.com...
Here's the thing: you do not have them already constructed.
If you don't initialize them in a constructor initializer list they'll be default constructed, and that's what you're currently experiencing.
The way to avoid that is to use a constructor initializer list. ;-)
Ok, two things spring to mind ... my first guess was that the assignment operator was not implemented but I discounte this straight away because of the "rule of three". After all, the authors of the library are experienced C++ programmers - plus because it is open sourced, someone would have spotted such an oversight straight away.
As you will see from my code comments, I pointted that the variables will be constructed via their "default ctors". The point which (you still seem to be missing) is taht there is valuable information passed to the LogWriter ctor - and I do not have this information at the point of the declaration/definition of the private variables. So the question must remain, How do I create (for example), an instance of a SharedAppender pointer, using the pointer returned by the expression " new RollingFileAp pender("myLogFi le.log")" ?
This is what I have been trying to find out all along. thanks
Having the declaration of an object in your class does _not_ "default construct" the object. Member objects are constructed at run-time, during the construction of the object containing them. And you can control that construction by adding them to an initialization list in your LogWriter constructor. They are only default constructed if you _don't_ put them in the initializer list. (And even the, it's during the run-time construction of an instance of the LogWriter object.) You might want to read up on initializer lists, especially how they are used for member object and base class object construction.
Also, if for some reason you _do_ need to create members _after_ construction of the containing object, then make them member pointers, not member objects, and add appropriate new and delete calls.
One more thing: notice that m_Appender appears to be a pointer. When the LogWriter object is constructed, no construction takes place of any object for it unless you write the code to do so (via a call to new), despite your code comment that it's default constructed at that point.
-Howard
Many Thanks Howard (and Alf), I finally solved it (yes - by using
initializer lists). Makes for some pretty terse looking statements - but
it works !
Many thanks for your expertise and patience ! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Ron |
last post by:
It has been a while since I last programmed in java, I am getting an error
trying to instaniate one of my classes,
public class teamStats{
String list;
public void teamStats(String newlist){
list=newlist;
}
}
|
by: Kamliesh Nadar |
last post by:
Hi
I am developing a software using VB.NET I am facing
following problems -
1. Though I have placed the NotifyIcon control on the
window service application, after starting the service I
am not able to view the icon on the system tray.
2. Secondly how to add a new item in the Window explorer
|
by: el prinCipante |
last post by:
I'm getting tired of the following error message.
Compiler Error message : Error: Need explicit cast to convert
from: float
to: float *
I am trying to use a routine from the Numerical Recipes library called
amebsa.c. The routine requires several parameters of the following
form. *iter, **p, *yb.... . How does one initialize these variables?
If I declare them as pointer (in this case, *yb and *iter) the
compiler still tells me that it...
|
by: smanicom |
last post by:
Hi all
I need help urgently as I am in the middle of a migration and can't resolve a TNS error.
My db has been fine for months! Today I connected as sysdba and shut down the db. Now I am trying to bring it back up but when i try connect as sysdba I get ORA-12154: TNS:could not resolve service name
This is odd because I have not changed the tnsnames file at all. Please help
Am I missing something simple here?
|
by: sam_cit |
last post by:
Hi Everyone,
I have the following unit to explain the problem that i have,
class sample
{
public : sample()
{
printf("in sample...\n");
}
| |
by: janakivenk |
last post by:
Hello,
I am running Oracle 10g R2 in our office. I created the following procedure. It is suppose to access an xml file ( family.xml). The procedure is compiled and when I try to run it, i get the following error.
SQL> execute domsample('c:\','song.xml','error.txt')
BEGIN domsample('c:\','song.xml','error.txt
*
ERROR at line 1:
ORA-31001: Invalid resource handle or path
|
by: =?Utf-8?B?TWF0dA==?= |
last post by:
I'm having a problem with a static class constructor being called twice. I
have the static class MasterTaskList which uses a BackgroundWorker to execute
multiple methods on a separate thread. The static constructor calls a reset
function which creates a new instance of BackgroundWorker and attaches the
appropriate event handlers. There is also a static method ReportProgress for
the called methods to do just that.
What is happening is...
|
by: Robert Wells |
last post by:
Gentlemen,
We are looking for two IBM documents that are needed urgently for a
project. They are titled "4680 Store Systems Serial I/O Channel Attachment
Information" and "Serial I/O Product Attachment Information". $100 will be
paid to the first person to deliver either or both of these documents in
their entirety. If you have partial documents or you are not sure please
contact US anyway. The contact person is ty@rocketfarm.com
...
|
by: uhdam |
last post by:
Hi
I am unable to access a non static variable in a static context
The variable is an arrayList and in a separate file
I cannot change the corresponding non-static class into static class
Please help me to resolve this problem
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |