#include <date/date.h> invokes numerous errors - c++

I am trying to show in an edit box a program execution time. I have found some examples at Stackoverflow like the code here below.
using namespace std;
using namespace date;
ostringstream out;
auto start = chrono::system_clock::now();
//some program execution
auto finish = chrono::system_clock::now();
out << finish - start;
string s = out.str();
cout << s << '\n';
I have installed the library #include <date/date.h> via vcpkg. But the problem is: as soon as I do #include <date/date.h>, and run code with Local Windows Debugger a numerous mistake is happening as indicated in the snap shot below.
I mean, simple including of <date/date.h> library leads to errors.
How can I avoid this issue?
Many thanks in advance!
UPD
#include "pch.h"
#include "framework.h"
#include "MFCApplication2.h"
#include "MFCApplication2Dlg.h"
#include "afxdialogex.h"
#include <iostream>
#include <date/date.h>
void CMFCApplication2Dlg::OnBnClickedButton1()
{
//I have cleared the code inside, but errors yet appear
}

Related

"name followed by '::' must be a class or namespace name" error in release mode but not debug mode

C++, Visual Studio 2019
I keep getting this error in release mode but not debug mode for lines that use the "filesystem" and "chrono" libraries. I'm thinking that the issue probably relates to how the header files are linked in release mode.
Below are the relevant snippets of my code (ellipses indicate omitted code). The red underline of the error is under "chrono" and "filesystem", respectively.
#include <iostream>
#include <vector>
#include <fstream>
#include <numeric>
#include <string>
#include <algorithm>
#include <ctime>
#include <iterator>
#include <filesystem>
#include <cmath>
using namespace std;
...
int main() {
auto startTime = chrono::steady_clock::now(); // ERROR HERE
...
for (auto& entry : filesystem::directory_iterator(AbsPath + PTS_ByIndustry_Path)) { // ERROR HERE
if (find(TS_ToIncludeNums.begin(), TS_ToIncludeNums.end(), i) != TS_ToIncludeNums.end()) {
string p = entry.path().u8string();
PTS_ToInclude.push_back(p);
}
i++;
}
...
}
Fixed already! In project properties, I was changing the language standard to C++17 for Debug Mode but not Release Mode.
Screenshot:
.
It seems you didn't import the chrono header.
#include <chrono>

readdir hangs up if libpocofoundation is linked with my tiny app

I've started debbuging on some app, which hangs up in a loop based on readdir call.
Step by step I've cut everything but problem code, this is it:
So, in basic, it shows name of first entry and nothing more. It even does not exits, just waiting for something.
Also, I've found, that if don't lin it against libpocofoundation, it works.
But I have to do it because it used in the original app.
I'm a little bit confused, I don't use Poco in this example in any way, but it some way hangs it.
Please help me, I'm in panic :D
#include <iostream>
#include <sys/types.h>
#include <dirent.h>
#include <cstring>
#include <string>
#include <fcntl.h>
using namespace std;
int main(int argc, char *argv[])
{
const char TMP_DIR[] = "/opt";
DIR *dir = opendir(TMP_DIR);
std::cerr
<< readdir(dir)->d_name
<< readdir(dir)->d_name
<< std::endl;
return 0;
}
So... I don't know why it was happening. So I just dropped libpoco.

Building in Visual Studio not recognizing getline() or stoi() even with #include <string> and <fstream> statements

I've been trying to learn C++ over the past couple of days, and ran into a problem when I was trying to use the getline() and stoi() methods in some practice code:
#include <string>
#include <fstream>
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
string numberGuessed;
int intNumberGuessed = 0;
do {
cout << "Guess a number between 1 and 10";
getline(cin, numberGuessed);
intNumberGuessed = (stoi(numberGuessed));
cout << intNumberGuessed << "\n";
} while (intNumberGuessed != 4);
cout << "You win\n";
return 0;
}`
When I tried to build this code in VS 2015, the console could not identify getline or stoi as if I hadn't added #include statements for string and fstream. Is there something wrong with my code or is it something to do with VS?
It's something to do with VS.
Since you have
#include "stdafx.h"
I'm guessing you have precompiled headers turned on, and "stdafx.h" is the precompiled header. (That's the default name in VS)
With precompiled headers turned on, anything before the include statement for the precompiled header is ignored.
Either make sure #include "stdafx.h" is the very first thing in the file (except for comments), or turn off precompiled headers.

Threads in c++ using eclipse juno

I'm trying to run the following code, but eclipse is giving the errors "type thread cannot be resolved", "thread was not declared in this scope", "expected ; before t1".
#include <iostream>
#include "Tetris.h"
#include <Windows.h> //Sleep
#include <conio.h> //getch()
#include <thread>
#define SLEEP_TIME 1000
using namespace std;
void call_from_thread() {
cout << "hello!" << endl;
}
int main(){
/* some code... */
thread t1(call_from_thread);
/* some more code... */
return 0;
}
So far i've tried the following:
I've also tried the -thread flag as I've seen someone suggesting in an answer here.
I've been looking on google and stackoverflow for hours without being able to solve the problem. "thread" still cannot be resolved and therefore I can't compile and generate a binary.
Any help would be very welcome
thanks in advance

srand(time(NULL)) "Function 'srand' could not be resolved."

I have been trying to debug this problem for a while and quite honestly, I just can't see what I'm doing wrong.
Why is there a syntax error?
#include <iostream>;
#include <time.h>;
#include <stdio.h>;
#include <stdlib.h>;
using namespace std;
class Problem3 {
public:
bool isPrime(long double num) {
srand(time(NULL));
return 0;
}
};
The error I'm getting is,
"Function 'srand' could not be resolved."
I'm well aware now that I don't need the semi-colons after 'include' statements
I'm using Eclipse CDT along with MinGW as my compiler
How I resolved the problem:
It had to do with the MinGW compiler I was using. Switching over to Visual Studio solved the problem.
; at the end of the #include directives are the problem in your code. #include directives don't need (wrong to place indeed) semicolons at the end unlike C++ statements.
[Warning] extra tokens at end of #include directive [enabled by default]
It seems any character after > in the directive causes this error/warning.
#include<iostream>a //error
Change to this:
#include <iostream>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
class Problem3 {
public:
bool isPrime(long double num) {
srand(time(NULL));
return 0;
}
};
int main(){
cout<<"Hello Main";
}
EDIT:
Regarding the linker issue:
One suggestion is C++ expects types to be explicitly casted between types (more than C). So, use a cast to convert time_t which is returned by the time to unsigned int which is the input parameter type of srand. (And of course this might not be the problem with linker error)
Instead of using stdlib.h, try using <cstdlib>, try if it helps. Because it uses namespace.
Apart from that, I have seen this snippet here. Use that pattern if it helps.
#include <cstdlib>
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
srand(time(0)); //use current time as seed for random generator
int random_variable = rand();
cout << "Random value on [0 " << RAND_MAX << "]: "
<< random_variable << '\n';
}
there is already question in SO check if that helps Eclipse Method could not be resolved in a simple program C++
Never use time() to initialize srand()..
EDIT:
Now it seems many people got this kind of problem. I found a question How do I fix Eclipse CDT Error “Function 'isdigit' could not be resolved. He is facing the same problem. The asker suggested a work around to this in his question edit.
Quoted from that question:
I now believe this to be a Code Analysis problem. A better solution is
to edit the Code Analysis options to make "Function could not be
resolved" be a warning instead of an error. That way you can see the
warnings in Problems view, but continue to work. If the function is
REALLY missing, the compiler will tell you! I also have a new theory,
that the problem is with the Code Analyzer following symlinks, because
all of the "missing" functions are in symlinked include files. Would
love any input on this theory.
Hope that points to solve the problem.
; should not be there after #include.
#include <iostream>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include files shoule not end with ;