System::DateTime ^now = System::DateTime::Now;
System::DateTime ^now2 = System::DateTime::Now;
System::TimeSpan ^span = now->Subtract(now2);
上面的代码会出现编译错误:
test.cpp(104) : error C2664: 'System::TimeSpan System::DateTime::Subtract(System::DateTime)' : cannot convert parameter 1 from 'System::DateTime ^' to 'System::DateTime'
1> No user-defined-conversion operator available, or
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
now和now2是c ++ / cli句柄,但该方法需要实际的对象。从c ++ / cli调用该方法的正确方法是什么?
答案 0 :(得分:4)
DateTime
和TimeSpan
是值类型。
System::DateTime now = System::DateTime::Now;
System::DateTime now2 = System::DateTime::Now;
System::TimeSpan span = now.Subtract(now2);
答案 1 :(得分:0)
System::Diagnostics::Stopwatch^ stopwatch = gcnew System::Diagnostics::Stopwatch();
stopwatch->Start();
// code
stopwatch->Stop();
// stopwatch->Elapsed