我试图在我的代码中处理日期和时间,并被指向boost库的方向-特别是boost::locale::date_time (部分原因是这让我避免了让我之前的实现变得困难的夏令时怪癖)。
然而,我得到了不一致的结果。当我在date_time对象中存储日期,然后尝试从它获取数据时,它是不正确的。下面是一个例子:
#include <boost\\asio\\error.hpp>
#include <boost\\locale.hpp>
using namespace std;
int main()
{
// Necessary to avoid bad_cast ex
我希望能够使用std::stringstream或boost::lexical_cast等标准技术来序列化我的C++类。
例如,如果我有一个Point对象(2,4),那么我想将它序列化为"(2,4)",并且还可以从这个字符串构造一个Point对象。
我已经有一些代码,但有一些问题。指向字符串可以工作,但有时输入不是完全从流中读取的。字符串到点的转换会导致bad_cast异常。
class Point
{
public:
Point() : mX(0), mY(0) {}
Point(int x, int y) : mX(x), mY(y){}
int x
我尝试使用boost::date_time将日期字符串(从Twitter API获得)解析为ptime对象。日期格式的示例如下:
Thu Mar 24 16:12:42 +0000 2011
但是,无论我做什么,在尝试解析字符串时都会出现"Year is out of valid range“异常。日期格式在我看来是正确的,下面是代码:
boost::posix_time::ptime created_time;
std::stringstream ss(created_string);
ss.exceptions(std::ios_base::failbit); //Turn on
我检查了dynamic_cast的行为,发现当它失败时,只有当目的地是引用类型时才会抛出std::bad_cast异常。如果目标是指针类型,则不会从强制转换中抛出异常。这是我的示例代码:
class A
{
public:
virtual ~A()
{
}
};
class B : public A
{
};
int main()
{
A* p = new A;
//Using reference
try
{
B& b = dynamic_cast<B&>(*p)
#include "boost/date_time/gregorian/gregorian.hpp"
int main()
{
boost::gregorian::greg_weekday dWeek(boost::date_time::Wednesday);
//Code One
// warning C4482: nonstandard extension used: enum 'boost::date_time::weekdays' used in qualified name
if (dWeek.as_enum()==
我一直在努力学习Boost.Locale模块。以为例。代码编译成功,但当我试图运行它时,它会出现错误。
Unhandled exception at 0x74d34b32 in LearningCPP.exe: Microsoft C++ exception: std::bad_cast at memory location 0x00a6fa48..
我也附加了程序的堆栈跟踪。下面是:
KernelBase.dll!74d34b32()
[Frames below may be incorrect and/or missing, no symbols loaded for
我正在努力弄清楚如何使用boost::date_time进行转换。我想将从Unix时代(1970年1月1日00:00)测量到的毫秒值转换为人类可读的字符串-例如:2/13/2012 15:20:11将是理想的。
我尝试了一些std C++ / boost的建议,我已经看到了,但还没有任何运气。下面是我使用的代码:
long long ticksFromEpoch = 1329117220501;
std::time_t tt = static_cast<time_t>(ticksFromEpoch);
boost::posix_time::ptime d
我有一个非常小的示例应用程序,它在Ubuntu上构建、链接和正确运行。但是,当我在OS上试用时,我会得到链接错误,但我不知道。
这是我的CMakeLists.txt文件:
PROJECT ( Test C CXX )
CMAKE_MINIMUM_REQUIRED ( VERSION 2.8 )
SET ( Boost_USE_STATIC_LIBS ON )
SET ( Boost_USE_MULTITHREADED ON )
SET ( Boost_USE_STATIC_RUNTIME ON )
FIND_PACKAGE ( Boost REQUIRED COMPONENTS regex s
我正在尝试使用boost date_time检查日期是否有效。文档说,如果日期无效,它将抛出异常。现在,如果日期确实无效,我一直在尝试使用try-catch,但不知何故,我的程序仍然被抛出并停止。
简单的测试代码:
#include "boost/date_time/gregorian/gregorian.hpp"
#include <iostream>
int main()
{
int year = 2013;
int month = 1;
int day = 50;
try
{
boost::grego