I am not sure how to get ride of this compiler error:
error C2676: binary '>=': 'std::chrono::system_clock::time_point'
#include <ctime>
#include <chrono>
int main()
{
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
std::time_t now_c = std::chrono::system_clock::to_time_t(now - std::chrono::hours(24));
if (std::chrono::system_clock::now() >= now_c)
{
}
}
Here is what the compiler outputs:
1>------ Build started: Project: test, Configuration: Debug x64 ------
1> Source.cpp
1>d:\dev\cpptests\test\test\source.cpp(25): error C2784: 'bool std::operator >=(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)': could not deduce template argument for 'const std::pair<_Ty1,_Ty2> &' from 'std::chrono::system_clock::time_point'
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\utility(311): note: see declaration of 'std::operator >='
1>d:\dev\cpptests\test\test\source.cpp(25): error C2784: 'bool std::chrono::operator >=(const std::chrono::duration<_Rep,_Period> &,const std::chrono::duration<_Rep2,_Period2> &)': could not deduce template argument for 'const std::chrono::duration<_Rep,_Period> &' from 'std::chrono::system_clock::time_point'
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\chrono(538): note: see declaration of 'std::chrono::operator >='
1>d:\dev\cpptests\test\test\source.cpp(25): error C2784: 'bool std::chrono::operator >=(const std::chrono::time_point<_Clock,_Duration> &,const std::chrono::time_point<_Clock,_Duration2> &)': could not deduce template argument for 'const std::chrono::time_point<_Clock,_Duration2> &' from 'time_t'
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\chrono(905): note: see declaration of 'std::chrono::operator >='
1>d:\dev\cpptests\test\test\source.cpp(25): error C2676: binary '>=': 'std::chrono::system_clock::time_point' does not define this operator or a conversion to a type acceptable to the predefined operator
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I recommend you to use type deduction with auto for this kind of case, as it makes the code much clearer. Also, as said in the comments above, the std::chrono facilities are not directly compatible with c-style time_t. I would recommend to keep using just std::chrono since it is more type-safe than it's counterpart.
#include <ctime>
#include <chrono>
#include <iostream>
int main()
{
auto now = std::chrono::system_clock::now();
auto now_c = now - std::chrono::hours(24);
if (std::chrono::system_clock::now() >= now_c)
{
std::cout << "it works!" << std::endl;
}
return 0;
}
You are try to compare a C++ time_point with a C time! And there is no operator >= to compare. Then you try to compare nanosecond with second
The time_point has a function named time_since_epoch and you can use it.
Using auto can help solve the problem but not understanding what happens and what is under the hood!
So you simply can compare(not good):
if ( now.time_since_epoch().count() >= now_c)
And the better code is:
std::chrono::duration_cast< std::chrono::seconds>(now.time_since_epoch()).count()
Because time_t is per second
if ( now.time_since_epoch().count() >= now_c){
std::cout << now.time_since_epoch().count() << '\n';
std::cout << std::chrono::duration_cast< std::chrono::seconds>(now.time_since_epoch()).count() << '\n';
std::cout << now_c << '\n';
}
the output:
1487879248873636085
1487879248
1487792848
Related
uchar szPlaintext[128]; //dato da criptare
cout << "\nInserisci testo : ";
getline(cin, szPlaintext);
I tried it with a getline (cin, szPlaintext); but I have a lot of errors. I compile with VS2015.
Premise that I am trying to implement an AES (not mine), to my program; the uchar declaration was in this way.
uchar szPlaintext [128] = "text that I want to insert";
The errors are:
1>d:\download\scuola\c++\virtualaes\virtualaes\encrypt.cpp(32): error C2672: 'getline': no matching overloaded function found
1>d:\download\scuola\c++\virtualaes\virtualaes\encrypt.cpp(32): error C2784: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &)': could not deduce template argument for 'std::basic_string<_Elem,_Traits,_Alloc> &' from 'uchar [128]'
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\string(157): note: see declaration of 'std::getline'
1>d:\download\scuola\c++\virtualaes\virtualaes\encrypt.cpp(32): error C2780: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem)': expects 3 arguments - 2 provided
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\string(146): note: see declaration of 'std::getline'
1>d:\download\scuola\c++\virtualaes\virtualaes\encrypt.cpp(32): error C2784: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &&,std::basic_string<_Elem,_Traits,_Alloc> &)': could not deduce template argument for 'std::basic_string<_Elem,_Traits,_Alloc> &' from 'uchar [128]'
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\string(126): note: see declaration of 'std::getline'
1>d:\download\scuola\c++\virtualaes\virtualaes\encrypt.cpp(32): error C2780: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &&,std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem)': expects 3 arguments - 2 provided
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\string(73): note: see declaration of 'std::getline'
Pietrob0b
Utente Junior
Messaggi: 36
Iscritto il: 10 dic 2015, 20:44
std::getline takes a std::string reference, not a raw pointer.
string sPlaintext;
cout << "\nInserisci testo : ";
getline(cin, sPlaintext);
uchar* szPlaintext = (uchar*) sPlaintext.c_str(); //dato da criptare
As others have already pointed out std::getline takes a reference of std::string not "char *" , so your program has failed to compile .
Below are the two version of std::getline .
istream& getline (istream& is, string& str, char delim);
istream& getline (istream& is, string& str);
Now , how to make it work assuming you want input string in char* .Follow the below program .
// Example program
#include <iostream>
#include <string>
#include<cstring>
int main()
{
char szPlaintext[128]; //dato da criptare
std::cout << "\nInserisci testo : ";
std::string str ;
getline(std::cin, str);
strcpy(szPlaintext,str.c_str());
std::cout << szPlaintext;
}
I didn't really know what to write in the title, but basically I have a single .cpp, with only standard library headers included and no "using" keywords. I made my own "generate(...)" function. After including the library, Visual Studio shows me an error (where the function is being called), basically saying that it doesn't know whether to choose std::generate(...) or generate(...) because they have matching argument lists.
Is this a bug or have I missed something? I might also add that I am using VS2015.
#include <iostream>
#include <ctime>
#include <vector>
#include <algorithm>
template<typename Iter, typename Function>
Function generate(Iter begin, Iter end, Function f)
{
while (begin != end)
{
*begin = f();
++begin;
}
return f;
}
class Random
{
public:
Random(int low, int high)
: mLow(low), mHigh(high)
{}
int operator()()
{
return mLow + rand() % (mHigh - mLow + 1);
}
private:
int mLow;
int mHigh;
};
class Print
{
void operator()(int t)
{
std::cout << t << " ";
}
};
int main()
{
srand(time(0));
std::vector<int> intVec;
intVec.resize(15);
Random r(2, 7);
generate(intVec.begin(), intVec.end(), r);
}
Error output:
1>------ Build started: Project: Functor, Configuration: Debug Win32 ------
1> Main.cpp
1>c:\users\michael sund\documents\visual studio 2015\projects\gi_cpp\functor\main.cpp(44): warning C4244: 'argument': conversion from 'time_t' to 'unsigned int', possible loss of data
1>c:\users\michael sund\documents\visual studio 2015\projects\gi_cpp\functor\main.cpp(50): error C2668: 'generate': ambiguous call to overloaded function
1> c:\users\michael sund\documents\visual studio 2015\projects\gi_cpp\functor\main.cpp(7): note: could be 'Function generate<std::_Vector_iterator<std::_Vector_val<std::_Simple_types<int>>>,Random>(Iter,Iter,Function)'
1> with
1> [
1> Function=Random,
1> Iter=std::_Vector_iterator<std::_Vector_val<std::_Simple_types<int>>>
1> ]
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\algorithm(1532): note: or 'void std::generate<std::_Vector_iterator<std::_Vector_val<std::_Simple_types<int>>>,Random>(_FwdIt,_FwdIt,_Fn0)' [found using argument-dependent lookup]
1> with
1> [
1> _FwdIt=std::_Vector_iterator<std::_Vector_val<std::_Simple_types<int>>>,
1> _Fn0=Random
1> ]
1> c:\users\michael sund\documents\visual studio 2015\projects\gi_cpp\functor\main.cpp(50): note: while trying to match the argument list '(std::_Vector_iterator<std::_Vector_val<std::_Simple_types<int>>>, std::_Vector_iterator<std::_Vector_val<std::_Simple_types<int>>>, Random)'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
This happens on not just VC++ (VS 2015), but g++ 4.9+ as well. The issue here is the tricky Argument Dependent Lookup (Koenig Lookup).
It looks at the two iterators you're adding and it sees the "generate" function in std because the iterators also come from the std namespace (this is the point of Argument Dependent Lookup).
This problem actually bit me at one point: when I wrote my own tie implementation that did a few things extra to tie. I had to call mine tye because Koenig Lookup caused the considered overloads to be equal in their ranking and thus cause an error like this.
Either prefix generate with :: to start lookup from the global namespace (::generate( vec.begin(), vec.end(), ... );), or name it differently.
The following used to build OK on VS2008 but on VS2013, it moans. OK in g++.
#include <sstream>
int main()
{
int a = 2, b = 5;
std::ostringstream oss;
oss << a == 0 ? a : b;
}
The error message is
1>u:\precedence\precedence.cpp(7): error C2678: binary '==' : no operator found which takes a left-hand operand of type 'std::basic_ostream<char,std::char_traits<char>>' (or there is no acceptable conversion)
1> c:\program files\microsoft visual studio 12.0\vc\include\exception(497): could be 'bool std::operator ==(const std::exception_ptr &,const std::exception_ptr &)' [found using argument-dependent lookup]
1> c:\program files\microsoft visual studio 12.0\vc\include\exception(502): or 'bool std::operator ==(std::nullptr_t,const std::exception_ptr &)' [found using argument-dependent lookup]
1> c:\program files\microsoft visual studio 12.0\vc\include\exception(507): or 'bool std::operator ==(const std::exception_ptr &,std::nullptr_t)' [found using argument-dependent lookup]
1> c:\program files\microsoft visual studio 12.0\vc\include\system_error(402): or 'bool std::operator ==(const std::error_code &,const std::error_condition &) throw()' [found using argument-dependent lookup]
1> c:\program files\microsoft visual studio 12.0\vc\include\system_error(410): or 'bool std::operator ==(const std::error_condition &,const std::error_code &) throw()' [found using argument-dependent lookup]
1> while trying to match the argument list '(std::basic_ostream<char,std::char_traits<char>>, int)'
It works on both versions if the output statement is changed to
oss << (a == 0? a: b);
I can force a different error in VS2008 if a constant is not used: something like
int zero = 0;
oss << a == zero? a: b;
Just wondering why there is an error when a constant is not used.
Edit Managed to get an warning with g++ with -Wall.
The expression the compiler sees due to the precedence is, in all cases:
((oss << a) == 0) ? a : b;
Now oss << a returns oss. How can you compare oss with 0? It has an implicit conversion operator operator void *.
VS2013 presumably replaced that with explicit operator bool for C++11 (the whole point of which was so that these hard-to-find bugs don't occur). libstdc++ has not done this replacement to my knowledge. It must in order to conform with C++11.
Now why doesn't zero compile? zero is not a null pointer constant, but 0 is. Thus, the latter can be compared to the pointer returned by operator void *.
In any case, the simple way to fix this so it should work properly with any conforming implementation is to add parentheses:
oss << (a == 0 ? a : b);
It appears that the compiler is interpreting it different.
Based on the error, I would suspect that the compiler is interpreting the line of code like this
(oss << a) == 0 ? a : b;
So when you put in the parens there was no longer any way it could interpret it that way, and you got the expected results.
I suspect that this would also work.
oss << (a == 0) ? a : b;
I get a following compiler (vs2012) error:
Error 3 error C2679: binary '+=' : no operator found which takes a
right-hand operand of type 'const std::chrono::duration<_Rep,_Period>'
(or there is no acceptable conversion) c:\program files
(x86)\microsoft visual studio 11.0\vc\include\chrono 749
My definition of duration is:
// Tick interval type in nanoseconds
typedef std::chrono::duration<double, std::ratio<1, 100000000>> tick_interval_type;
Same error when I use float... It only compiles when the Rep type of duration is integer.
Can someone please help?
Edit (more complete log from Output):
c:\program files (x86)\microsoft visual studio
11.0\vc\include\chrono(749): error C2679: binary '+=' : no operator found which takes a right-hand operand of type 'const
std::chrono::duration<_Rep,_Period>' (or there is no acceptable
conversion) with [
_Rep=double,
_Period=std::nano ] c:\program files (x86)\microsoft visual studio 11.0\vc\include\chrono(166): could be 'std::chrono::duration<_Rep,_Period>
&std::chrono::duration<_Rep,_Period>::operator +=(const
std::chrono::duration<_Rep,_Period> &)' with [
_Rep=__int64,
_Period=std::nano ] while trying to match the argument list '(std::chrono::nanoseconds, const
std::chrono::duration<_Rep,_Period>)' with [
_Rep=double,
_Period=std::nano ] c:\program files (x86)\microsoft visual studio 11.0\vc\include\thread(164) : see reference to function template instantiation 'xtime std::_To_xtime(const
std::chrono::duration<_Rep,_Period> &)' being compiled with [
_Rep=double,
_Period=std::nano ] c:\dev\projects\revolverx\classes\ticker.h(78) : see reference to function template instantiation 'void
std::this_thread::sleep_for(const
std::chrono::duration<_Rep,_Period> &)' being compiled with [
_Rep=double,
_Period=std::nano ]
<chrono> in Visual Studio is broken. It doesn't work with mixed type arithmetic, which, arguably, is one of the main features of <chrono>. You get this error because one of the sides uses __int64 nanos and the other uses double nanos.
I recommend either dropping it in favor of a real C++ implementation, or using Boost.Chrono.
Update: I've come upon this question four years after it was originally posted, and I've tested this on Visual Studio 2015. It does compile now. For example:
#include <iostream>
#include <iomanip>
#include <chrono>
int main()
{
typedef std::chrono::duration<double, std::ratio<1, 100000000>> tick_interval_type; // Originally posted line
tick_interval_type tick {0};
tick += std::chrono::microseconds(3);
std::cout << std::setprecision(6) << std::fixed << tick.count();
return 0;
}
Output:
300.000000
#include <iostream>
#include <string>
#include <regex>
#include <ios>
#include <locale>
using namespace std;
int main ()
{
const wstring wstr(L"<(.|\\n)*?>");
static const wregex wr(wstr);
wstring line (L"<tag>Random text<tag>");
wstring line2 (L"");
wcout << regex_replace<wchar_t>(line,wr,line2) << endl;
}
Compiler says:
ClCompile:
html.cpp
c:\users\usr\documents\visual studio 2010\projects\html\html\html.cpp(34): error C2784: std::basic_string<_Elem> std::tr1::regex_replace(const std::basic_string<_Elem> &,const std::tr1::basic_regex<_Elem,_RxTraits> &,const std::basic_string<_Elem> &,std::tr1::regex_constants::match_flag_type): not able to output argument template "const std::tr1::basic_regex<_Elem,wchar_t> &" from "const std::tr1::wregex"
c:\program files\microsoft visual studio 10.0\vc\include\regex(2739): look to typedef "std::tr1::regex_replace"
c:\users\usr\documents\visual studio 2010\projects\html\html\html.cpp(34): error C2784: std::basic_string<_Elem> std::tr1::regex_replace(const std::basic_string<_Elem> &,const std::tr1::basic_regex<_Elem,_RxTraits> &,const std::basic_string<_Elem> &,std::tr1::regex_constants::match_flag_type): not able to output argument template for "const std::tr1::basic_regex<_Elem,wchar_t> &" from "const std::tr1::wregex"
c:\program files\microsoft visual studio 10.0\vc\include\regex(2739): look to typedef of "std::tr1::regex_replace"
c:\users\usr\documents\visual studio 2010\projects\html\html\html.cpp(34): error C2784: std::basic_string<_Elem> std::tr1::regex_replace(const std::basic_string<_Elem> &,const std::tr1::basic_regex<_Elem,_RxTraits> &,const std::basic_string<_Elem> &,std::tr1::regex_constants::match_flag_type): not able to output argument template for "const std::tr1::basic_regex<_Elem,wchar_t> &" from "const std::tr1::wregex"
c:\program files\microsoft visual studio 10.0\vc\include\regex(2739): look to typedef "std::tr1::regex_replace"
Partial answer:
You should use regex_repalce this way:
wcout << regex_replace(line,wr,line2) << endl;
i.e., without the wchar_t. The first argument is for the Element Traits class, which you'd only rarely want to modify.
Edit
I've checked your code with VC++ 2010. Changing the line as I specified allowed the code to compile, and return the result as expected. Can you try it again?