Boost Date_Time problem compiling a simple program - c++

I'm writing a very stupid program using Boost Date_Time library.
int main(int srgc, char** argv) {
using namespace boost::posix_time;
date d(2002,Feb,1); //an arbitrary date
ptime t1(d, hours(5)+nanosec(100)); //date + time of day offset
ptime t2 = t1 - minutes(4)+seconds(2);
ptime now = second_clock::local_time(); //use the clock
date today = now.date(); //Get the date part out of the time
}
Well I cannot compile it, compiler does not recognize a type...
Well I used many features of Boost libs like serialization and more... I correctly built them and, looking in my /usr/local/lib folder I can see that libboost_date_time.so is there (a good sign which means I was able to build that library)
When I compile I write the following:
g++ -lboost_date_time main.cpp
But the errors it showed me when I specify the lib are the same of those ones where I do not specify any lib.
What is this? Anyone knows?
The error is
main.cpp: In function ‘int main(int,
char**)’: main.cpp:9: error: ‘date’
was not declared in this scope
main.cpp:9: error: expected ‘;’ before
‘d’ main.cpp:10: error: ‘d’ was not
declared in this scope main.cpp:10:
error: ‘nanosec’ was not declared in
this scope main.cpp:13: error:
expected ‘;’ before ‘today’

Though I can't figure out what's ss in your code,
qualifying date and Feb as the following will make your code valid.
boost::gregorian::date
boost::date_time::Feb
Hope this helps.

Related

extern main declaration from bsplib returns error

Im setting up the bsplib (https://github.com/Zefiros-Software/BSPLib) on a windows system (in VS Code) using WSL. When compiling I get the error message:
test.cpp:4:5: error: conflicting declaration of C function ‘int main()’
4 | int main()
| ^~~~
In file included from /mnt/d/study/software/bsp/include/bsp/bspExt.h:30,
from /mnt/d/study/software/bsp/include/bsp/bsp.h:34,
from test.cpp:2:
/mnt/d/study/software/bsp/include/bsp/bspClass.h:59:12: note: previous declaration ‘int main(int, char**)’
59 | extern int main(int argc, char **argv);
The program is used is just a bare example for BSP:
#include <iostream>
#include "bsp/bsp.h"
int main()
{
bsp_begin(bsp_nprocs());
int s = bsp_pid();
int p = bsp_nprocs();
printf("Hello World from processor %d / %d", s, p);
bsp_end();
return 0;
}
Compiled with:
g++ -I/mnt/d/study/software/bsp/include -g -lpthread -o main test.cpp
To my (quite limited) knowledge, the 'extern' in the header file should prevent the compiler from labelling the main as 'duplicate' of some sort. Im mostly interested in some of BSPs functionalities as part of a class of mine, that sadly does not include any support on the installation. What I've done so far:
Copied the include files from the repo
Added the include path to the compilation (-I Flag) and the -lpthread as instructed by the class script
Added the include path to the configuration (c_cpp_properties.json) [tested both with and without this, no difference]
Due to the many possible sources of that error (program, compiler, wsl, library, configuration, vs code, my stupidity) I cant determine where I am mistaken, nor am I able to find online resources to that combination.

Not able to build Raft implementation of LogCabin using scons and Protobuf 3

I am not able to build RAFT implementation of LogCabin (C++) using scons and protobuf3 in Ubuntu.
Errors be like
usr/local/include/google/protobuf/repeated_field.h: In member function ‘int google::protobuf::RepeatedPtrField<Element>::SpaceUsedExcludingSelf() const’:
/usr/local/include/google/protobuf/repeated_field.h:888:12: error: ‘ToIntSize’ is not a member of ‘google::protobuf::internal’
return internal::ToIntSize(SpaceUsedExcludingSelfLong());
^
In file included from /usr/local/include/google/protobuf/map.h:48:0,from /usr/local/include/google/protobuf/generated_message_table_driven.h:34, from ./build/Protocol/ServerStats.pb.h:25, from build/Tree/Tree.cc:19:
/usr/local/include/google/protobuf/map_type_handler.h: In static member function ‘static void google::protobuf::internal::MapTypeHandler<(google::protobuf::internal::WireFormatLite::FieldType)9u, Type>::Clear(google::protobuf::internal::MapTypeHandler<(google::protobuf::internal::WireFormatLite::FieldType)9u, Type>::TypeOnMemory*, google::protobuf::Arena*)’:
/usr/local/include/google/protobuf/map_type_handler.h:638:1: error: ‘GetEmptyStringAlreadyInited’ is not a member of ‘google::protobuf::internal’
STRING_OR_BYTES_HANDLER_FUNCTIONS(STRING)
Please suggest how do I proceed.!

gettimeofday() not declared in this scope - Cygwin

Can cygwin make use of gettimeofday()? I'm getting the error:
lab8.cpp: In function ‘int main()’:
lab8.cpp:62:30: error: ‘gettimeofday’ was not declared in this scope
gettimeofday(&start, NULL);
^
makefile:14: recipe for target 'lab8.o' failed
make: *** [lab8.o] Error 1
I definitely included <sys/time.h>. Is there a package that I have to install or does it just not work on Windows?
EDIT: Here's a simple test that yields the same error:
#include <sys/time.h>
int main() {
struct timeval start;
gettimeofday(&start, NULL);
}
With the error:
$ g++ -c -Wall -g -std=c++11 test.cpp -o test.o
test.cpp: In function ‘int main()’:
test.cpp:6:30: error: ‘gettimeofday’ was not declared in this scope
gettimeofday(&start, NULL);
You need to define _BSD_SOURCE to include the declaration gettimeofday (since glibc2.2.2 according to link below)
#define _BSD_SOURCE
#include <sys/time.h>
int main() {
struct timeval start;
gettimeofday(&start, NULL);
}
gettimeofday() - Unix, Linux System Call
Thank you John - your question game me my answer!
In case it helps anyone else searching on this error...
I got the same error:
error: ‘gettimeofday’ was not declared in this scope
gettimeofday(&lastBigTick, NULL);
This error started happening when I moved from Debian 8 (jessie) to Debian 9 (stretch) on a raspberry pi
For me, I had NOT #included "sys/time.h" I had only #included "time.h"
Not sure how it compiled before, but it did, so adding
#include "sys/time.h"
did the job for me.
Note: "time.h" is different and is still needed for other time functions, so I ended up needing both:
#include "time.h"
#include "sys/time.h"
My situation was perhaps different, as adding
#define _BSD_SOURCE
made no difference in my case - ie this was not the issue for me.

boost/lexical_cast on fedora 20

I'am trying to compile and run a C++ program where is included boost/lexical_cast.hpp in fedora 20, where is installed boost-devel 1.50
What I get is as follow:
ina#localhost Examples]$ g++ -I ../Libraries/ quark_prop.cpp
In file included from ../Libraries/mdp.h:177:0,
from ../Libraries/fermiqcd.h:15,
from quark_prop.cpp:1:
../Libraries/mdp_utils.h:73:51: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
int is_file(string filename, char permission[]="r") {
^
In file included from ../Libraries/fermiqcd.h:15:0,
from quark_prop.cpp:1:
/usr/include/boost/assert.hpp: In function ‘void boost::assertion::detail::assertion_failed_msg(const char*, const char*, const char*, const char*, long int)’:
../Libraries/mdp.h:49:14: error: expected unqualified-id before string constant
#define endl "\n"
^
../Libraries/mdp.h:49:14: error: expected ‘;’ before string constant
While in another pc with OS ubuntu 10.04 and boost 1.40 this codes works perfectly.
Any idea of what is happening?
Thank you
Your message is unrelated to boost.
You cannot pass a string literal as char*. It's always const, so pass it as char const*: Live On Coliru
Regarding the other error in mdp.h, you need to show the relevant code
UPDATE Ah.
The problem is with the define. It's breaking the compilation of the boost header because mdp.h writes;
std::endl
somewhere, and the preprocessor is making that into
std::"\n"
which isn't valid C++.
Remove the define. Use using instead (but not in header files):
http://en.cppreference.com/w/cpp/language/using_declaration
Why is "using namespace std" considered bad practice?

C++11 : error: ‘begin’ is not a member of ‘std’

I am trying to do the following operation:
source = new int[10];
dest = new int[10];
std::copy( std::begin(source), std::end(source), std::begin(dest));
However, the compiler reports the following error.
copy.cpp:5434:14: error: ‘begin’ is not a member of ‘std’
copy.cpp:5434:44: error: ‘end’ is not a member of ‘std’
copy.cpp:5434:72: error: ‘begin’ is not a member of ‘std’
I have included the required <iterator> header in the code. Can anybody help me on this?
Template functions std::begin() and std::end() are not implemented for pointers (pointers do not contain information about the number of elements they refer to) Instead them you should write
std::copy( source, source + 10, dest);
As for the error you should check whether you included header
#include <iterator>
Also maybe your compiler does not support the C++ 2011 Standard.
In addition to include <iterator>in C++11 enabled compiler. You should know begin/end are not useful for pointers, they're useful for arrays:
int source[10];
int dest[10];
std::copy(std::begin(source), std::end(source), std::begin(dest));
also have this problem when using g++ compiler this code in linux.
Using g++ compiler that contain C++ featuer should add C++11 flag
g++ -std=c++11 -o test test.cpp