Log4cplus not compiling as descirbed - c++

#include <log4cplus/configurator.h>
#include <log4cplus/fileappender.h>
#include <log4cplus/initializer.h>
#include <log4cplus/layout.h>
#include <log4cplus/logger.h>
#include <log4cplus/loggingmacros.h>
#include <string>
int main(int argc, char* argv[]) {
log4cplus::SharedAppenderPtr appender(new log4cplus::FileAppender("test.log"));
appender->setName("mainLog");
std::string pattern = "[%-5p][%D{%Y/%m/%d %H:%M:%S:%q}][%-l][%t] %m%n";
std::auto_ptr<log4cplus::Layout> layout(new log4cplus::PatternLayout(pattern));
appender->setLayout(layout);
mainLog = log4cplus::Logger::getInstance(LOG4CPLUS_TEXT("mainLog"));
mainLog.addAppender(appender);
LOG4CPLUS_INFO(mainLog, LOG4CPLUS_TEXT("Execution started!"));
return 0;
}
The above code should work according to the every guide I have managed to find on the subject, but it won't compile. The line:
appender->setLayout(layout);
underscores layout and says:
cannot convert argument 1 from 'std::auto_ptr<log4cplus::Layout>' to 'std::unique_ptr<log4cplus::Layout,std::default_delete<_Ty>>
What gives?

Related

How do I fix this c++ expected expression error?

I am making something in c++, it doesn't have any errors visible in Visual Studio code, but when I use g++ to be able to execute it, I get this error:
In file included from Main.cpp:6: In file included from ./Filechange/Filechange.hpp:1: ./Filechange/Filechange.cpp:14:24: error: expected expression
std::thread first ([&wtime,&f,&fn]() mutable {
^ Main.cpp:16:33: error: expected expression
OnFilechange("FileEvent", 0.5, [](char* txt){
^ 2 errors generated.
These are the files:
Main.cpp:
#include <lua.hpp>
#include <iostream>
#include <chrono>
#include <thread>
#include <stdio.h>
#include "Filechange/Filechange.hpp"
void wait(int seconds)
{
std::this_thread::sleep_for(std::chrono::seconds(seconds));
}
int main(int argc, char const *argv[])
{
lua_State *State = luaL_newstate();
OnFilechange("FileEvent", 0.5, [](char* txt){
std::cout << txt << std::endl;
});
lua_close(State);
return 0;
}
Filechange.cpp:
#include <thread>
#include <stdio.h>
#include <stdlib.h>
#include <chrono>
#include <string>
#include <fstream>
char* StringToChar(std::string str){
char* Array = new char[str.length() + 1];
strcpy(Array,str.c_str());
return Array;
}
void OnFilechange(const char *f, float wtime, void (*fn)(char* txt)){
std::thread first ([&wtime,&f,&fn]() mutable {
std::ifstream file(f);
std::string str;
std::string filecontents;
while (std::getline(file,str)){
filecontents += str;
filecontents.push_back('\n');
}
char* LastContents = StringToChar(filecontents);
char* CurrentContents = StringToChar(filecontents);
while (true){
if (wtime != 0){
std::this_thread::sleep_for(std::chrono::milliseconds(int(wtime*1000)));
}
filecontents = "";
while (std::getline(file,str)){
filecontents += str;
filecontents.push_back('\n');
}
CurrentContents = StringToChar(filecontents);
if (strcmp(LastContents, CurrentContents) != 0){
LastContents = StringToChar(filecontents);
fn(StringToChar(filecontents));
}
}
});
}
Filechange.hpp:
#include "Filechange.cpp"
#ifndef FILECHANGE_HPP
#define FILECHANGE_HPP
#include <thread>
#include <stdio.h>
#include <stdlib.h>
#include <chrono>
#include <string>
#include <fstream>
void OnFilechange(const char *f,float wtime,void (*fn)(char txt));
#endif
There's also a extension less file named FileEvent which will change in the runtime using other code files.
The Filechange.cpp and Filechange.hpp are in a folder named "Filechange"
This function:
void OnFilechange(const char *f, float wtime, void (*fn)(char* txt))
expects a function pointer, and a lambda in g++ is not implemented as a function pointer. Instead, you should declare the function to take a std::function, as in:
void OnFilechange(const char *f, float wtime, std::function<void(char *)> fn)
You may also need #include <functional> to get the declaration of std::function.
use -std=c++17 in g++ if possible as g++ defaulted to c++98

Invalid type of argument using pointer to struct array

I have a problem with the next code, I get the error: invalid type argument of unary '*' (have 'int'). If I write int *content that allows the code to run, but I have to write int=content and change the code *((ptab->content)+pC1+17), I tried but I can't fix the error.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include <iomanip>
#include <stdio.h>
#include <Windows.h>
using namespace std;
struct box{
int content;
};
struct box *ptab;
int pC1=5;
int main (){
ptab=new struct box[64];
if (*((ptab->content)+pC1+17)==0) {
pC1=pC1+17;
}
cout<<pC1<<endl;
}
I have to pass from pointers to poninters to struct arrays, this code is an example, because the original code has 23000 lines.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include <iomanip>
#include <stdio.h>
#include <Windows.h>
using namespace std;
int *box;
int pC1=5;
int main (){
box=new int[64];
if (*(box+pC1+17)==0){
pC1=pC1+17;
}
cout<<pC1<<endl;
}
With *((ptab->content)+pC1+17), an easier way to say it is ptab[pC1+17].content [the latter compiles and produces 22 as output]. Did you mean that or ptab->content + pC1 + 17 [which produces 5]?

Reference to "class" is ambigous

I would like to implement a hash table example.
So for this aim, I have created one header, one hash.cpp and main.cpp files.
in my hash.cpp , I tried to run a dummy hash function which takes key value and turns into an index value. however, it throws an error(reference to 'hash' is ambiguous) whenever I try to create an object according to that hash class.
this is my main.cpp:
#include "hash.h"
#include <iostream>
#include <cstdlib>
#include <string>
#include <stdio.h>
using namespace std;
int main(int argc, const char * argv[]) {
hash hash_object;
int index;
index=hash_object.hash("patrickkluivert");
cout<<"index="<<index<<endl;
return 0;
}
this is my hash.cpp:
#include "hash.h"
#include <iostream>
#include <cstdlib>
#include <string>
#include <stdio.h>
using namespace std;
int hash(string key){
int hash=0;
int index;
index=key.length();
return index;
}
this is my hash.h
#include <stdio.h>
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
#ifndef __hashtable__hash__
#define __hashtable__hash__
class hash
{
public:
int Hash(string key);
};
#endif /* defined(__hashtable__hash__) */
Your hash class symbol is clashing with std::hash
A quick fix could be using a global namespace qualifier
int main(int argc, const char * argv[]) {
::hash hash_object;
but a better and recommended one would be to stop polluting your global namespace with
using namespace std;
and just using std::cout or std::endl when you need them.
You could also create your own namespace in case you're writing a library.
Besides, you have some capital letter typos here:
index = hash_object.hash("patrickkluivert");
^ I suppose you're referring to the Hash() function here
and here
int Hash(std::string key) {
^ this needs to be capital as well
int hash = 0;
in case you want to match your declaration and avoid cast/linking errors.
Your hash class is conflicting with std::hash. Stop using using namespace std; right now. If you want to make print statements shorter, try using std::cout; using std::endl;

"Was not declared in this scope" error struct definition. c++

I'm thinking I have angered the "Header Guard" gods, but I don't see where. My program is laid out as follows:
(note :this is just the relevant info on these files)
main file:
#include "playlist.h"
#include "playlistitem.h"
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int main (int argc, char** argv)
//snip
PlayList allSongs;
//snip
playist.h:
#ifndef PLAYLIST_H
#define PLAYLIST_H
#include <iostream>
#include <string>
#include <vector>
#include "playlistitem.h"
#include "song.h"
#include "time.h"
struct Playlist {
std::vector<Song> songs;
Time cdTotalTime;
int totalTime;
};
plalist.cpp:
#include <iostream>
#include <string>
#include <vector>
#include "playlist.h"
song.h:
#ifndef SONG_H
#define SONG_H
#include <iostream>
#include <string>
#include "time.h"
struct Song {
std::string title;
std::string artist;
std::string album;
int track;
Time length;
};
song.cpp:
#include "song.h"
#include "csv.h"
#include <sstream>
#include <vector>
I get "Playlist was not declared in this scope" on line:
PlayList allSongs;
In my main file.
Thanks!
Check your capitalization.
Playlist and PlayList are being used.
You've just got your capitalization wrong... it's declared as Playlist, used as PlayList
clang's spell checking is helpful for this type of thing.
tmp.cpp:5:1: error: unknown type name 'PlayList'; did you mean 'Playlist'?
PlayList pl;
^~~~~~~~
Playlist
tmp.cpp:1:8: note: 'Playlist' declared here
struct Playlist {
^
1 error generated.

error: 'int main(int, char**)' previously defined here in C++

I'm implementing gtest now, and it gives me an error : main previously defined here.
Here's utest.cpp
// Bring in my package's API, which is what I'm testing
#include "../src/test.cpp"
// Bring in gtest
#include <gtest/gtest.h>
// Declare a test
TEST(TestSuite, testCase1)
{
EXPECT_EQ(5,getX(5));
}
// Run all the tests that were declared with TEST()
int main(int argc, char **argv){
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
And here's the code that i'm testing
test.cpp
#include "ros/ros.h"
#include "std_msgs/String.h"
#include <Project2Sample/R_ID.h>
#include <geometry_msgs/Twist.h>
#include <nav_msgs/Odometry.h>
#include <sensor_msgs/LaserScan.h>
#include <sstream>
#include "math.h"
int getX(int x)
{
return x;
}
int main(int argc, char **argv)
{
return 0;
}
There's nothing in test.cpp main but actual code will have some codes in main.
I dont have header files for utest and test cpp files
I tried
#ifndef UTEST_H
#define UTEST_H
and didn't solve the error.
The error message states what the problem is, you have two main() functions. I believe you want to remove the duplicate main() from test.cpp.