The variable in this class does not name a C ++ type
source link: https://www.codesd.com/item/the-variable-in-this-class-does-not-name-a-c-type.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
The variable in this class does not name a C ++ type
I am trying to use log4cpp in my program (*nix machine - Ubuntu 12.10, Codeblocks) to create logs at various instances in the program.
LoggerMain.h
#include "log4cpp/Category.hh"
#include "log4cpp/Appender.hh"
#include "log4cpp/FileAppender.hh"
#include "log4cpp/OstreamAppender.hh"
#include "log4cpp/Layout.hh"
#include "log4cpp/BasicLayout.hh"
#include "log4cpp/Priority.hh"
MainProgram.h
#include "LoggerMain.h"
MainProgram{
public:
log4cpp::Appender *appender;
};
MainProgram.cpp
appender = new log4cpp::FileAppender("default","program.log"); //getting error here
appender->setLayout(new log4cpp::BasicLayout());
log4cpp::Category& root = log4cpp::Category::getRoot();
main{
//will use root here for loggin
}
Error: src/MainProgram.cpp|21|error: ‘appender’ in ‘class MainProgram’ does not name a type|
What's going wrong? I am basically doing the same thing as in the log4cpp simple example documentation here
You can not have operations outside function, put below code into MainProgram construction,
MainProgram::MainProgram(){
appender = new log4cpp::FileAppender("default","program.log"); //getting error here
appender->setLayout(new log4cpp::BasicLayout());
log4cpp::Category& root = log4cpp::Category::getRoot();
}
You could use singleton mode for MainProgram
MainProgram{
public:
MainProgram* instance(){
if (!instance){
instance = new MainProgram();
return instance;
}
}
MainProgram()
{
appender = new new log4cpp::FileAppender("default","program.log"); //getting error here
appender->setLayout(new log4cpp::BasicLayout());
}
private:
log4cpp::Appender *appender;
MainProgram* instance;
};
int main(int argc, char* argv[])
{
MainProgram::Instance();
}
Note, C++ supports two forms of main functions, you need to change main function to one of below forms:
int main() { /* ... */ }
int main(int argc, char* argv[]) { /* ... */ }
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK