减去c+中的两个DateTime对象+/cli

减去c+中的两个DateTime对象+/cli,datetime,c++-cli,Datetime,C++ Cli,上面的代码获取编译器错误: System::DateTime ^now = System::DateTime::Now; System::DateTime ^now2 = System::DateTime::Now; System::TimeSpan ^span = now->Subtract(now2); now和now2是c++/cli句柄,但该方法需要实际对象。从c++/cli调用方法的正确方法是什么?DateTime和TimeSpan是值类型 test.cpp(104) : er

上面的代码获取编译器错误:

System::DateTime ^now = System::DateTime::Now;
System::DateTime ^now2 = System::DateTime::Now;
System::TimeSpan ^span = now->Subtract(now2);

now和now2是c++/cli句柄,但该方法需要实际对象。从c++/cli调用方法的正确方法是什么?

DateTime
TimeSpan
是值类型

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  

DateTime
TimeSpan
是值类型

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  


DateTime::Now是值类型,而Subtract返回值类型。在完全理解引用类型和值类型之间的差异之前,不要编写任何C++/CLI代码。任何关于.NET的书都会对此进行解释。DateTime::Now是一种值类型,而Subtract返回一种值类型。在完全理解引用类型和值类型之间的区别之前,不要编写任何C++/CLI代码。任何一本关于.NET的半正经的书都会解释它。我想更正确的是:
System::TimeSpan=now2.Subtract(现在)我猜更正确:
System::TimeSpan=now2.Subtract(现在)
System::Diagnostics::Stopwatch^ stopwatch = gcnew System::Diagnostics::Stopwatch();
stopwatch->Start();
// code
stopwatch->Stop();
// stopwatch->Elapsed