在Visual Studio 2012中使用Boost posix日期和时间编译错误

时间:2014-04-23 14:23:47

标签: c++ visual-studio-2012 boost

我尝试使用std :: stringstream对象初始化Boost ptime对象的实例。我从boost / date_time / format_date_parser.hpp收到编译错误,说明从int转换为unsigned short时出错。下面是导致错误的函数的副本以及我在源文件中包含的标题。该函数的目的是确定我要发送到另一个进程的文件是否具有文件大小,并且是否比该进程使用的当前副本更新。导致编译器错误的代码行是" searchTime_ss>> searchTime;"

#include "stdafx.h"
#include <winsock2.h>
#include <stdlib.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <errno.h>

#include <locale>
#include <map>
#include <string>
#include <vector>
#include <iterator>
#include <iostream>
#include <sstream>

#include <boost/format.hpp>
#include <boost/filesystem.hpp>
#include <boost/assign/std/vector.hpp>
#include <boost/algorithm/string.hpp>
#include "boost/date_time/gregorian/gregorian.hpp"
#include "boost/date_time/posix_time/posix_time_types.hpp"
#include "boost/date_time/posix_time/posix_time.hpp"
#include <boost/date_time/posix_time/posix_time_io.hpp>
#include <boost/regex.hpp>

using namespace std;
using namespace boost::gregorian;
using namespace boost::assign;
using namespace boost::filesystem;
using namespace boost::algorithm;
using namespace boost::posix_time;
using namespace boost;

namespace boostfs = boost::filesystem;
namespace boostpt = boost::posix_time;

int CCon::checkFileStatus(std::string filename, std::string fileTime, bool dateTokenFound)
{
    int rval = 1;
    boostfs::path afile( filename );

    if( boostfs::file_size( afile ) <= 0 ) {
        rval = 0;
    } else
    if( dateTokenFound ) {

        std::time_t modtime = boostfs::last_write_time( afile );
        boostpt::ptime lastAccessTime = boostpt::from_time_t( modtime );
        boostpt::ptime searchTime(not_a_date_time);

        boostpt::time_input_facet *fin = new boostpt::time_input_facet( "%m/%d/%Y %H:%M" );
        std::stringstream searchTime_ss;  
        searchTime_ss.imbue(std::locale(std::locale(), fin));

        searchTime_ss << fileTime;
        searchTime_ss >> searchTime;

        std::stringstream lastAccessTime_ss;
        lastAccessTime_ss.imbue(std::locale(std::locale(), fin));
        lastAccessTime_ss << lastAccessTime;

        if( lastAccessTime < searchTime ) {
            rval = 0;
        } else {
            // log message that we are sending the file.
        }
    }
    return rval;
}

以下是我从Visual Studio 2012获得的编译器错误:

W:\boost\boost_1_55_0\boost/date_time/time_facet.hpp(935) : see reference to function template instantiation 'std::istreambuf_iterator<_Elem,_Traits> boost::date_time::time_input_facet<time_type,CharT>::get(InItrT &,InItrT &,std::ios_base &,time_type &,std::basic_string<_Elem,_Traits,_Alloc> &,bool) const' being compiled
with
[
_Elem=char,
_Traits=std::char_traits<char>,
time_type=boost::posix_time::ptime,
CharT=char,
InItrT=std::istreambuf_iterator<char,std::char_traits<char>>,
_Alloc=std::allocator<char>
]
CCon.cpp(7348) : see reference to class template instantiation 'boost::date_time::time_input_facet<time_type,CharT>' being compiled
with
[
time_type=boost::posix_time::ptime,
CharT=char
]
W:\boost\boost_1_55_0\boost/date_time/format_date_parser.hpp(66): warning C4245: 'initializing' : conversion from 'int' to 'unsigned short', signed/unsigned mismatch

这是来自format_date_parser.hpp(66)的行似乎是罪魁祸首 int_type i = -1;

我尝试了多种包含标题的组合,但没有任何帮助。我已经在Google上搜索了类似的问题/解决方案,但没有找到解决此错误的问题,这让我误认为问题出在我的代码中,但我无法找到它。在CentOS和gcc下运行的代码非常相似,没有问题。任何帮助是极大的赞赏!谢谢!

1 个答案:

答案 0 :(得分:0)

我认为它已被修复,因为现在有了最新的提升,我可以看到static_cast:

int_type i = static_cast<int_type>(-1);