c++ - 错误打印 posix_time

标签 c++ boost posix

我在尝试编译这段代码时遇到了这些奇怪的编译错误。

OutputHandler.h:

#pragma once

#include <fstream>
#include <boost/thread/mutex.hpp>

#include "FileNotAccessibleException.hpp"

class OutputHandler {
public:
    enum WarningLevel {INFO, WARNING, ERROR, CRASH};

    OutputHandler(std::string const& pathAsString) throw(FileNotAccessibleException);
    void log(std::string const& message, WarningLevel const& warningLevel);
    void log(std::exception const& exception, WarningLevel const& warningLevel);

private:
    std::fstream file;
    std::string path;
    boost::mutex mutex;
    static char const * const errorPrefixes[];
};

OutputHandler.cpp:

#include "OutputHandler.h"

OutputHandler::OutputHandler(std::string const& pathAsString) throw(FileNotAccessibleException) : path(pathAsString) {
    file.open(path, std::ios::app);
    if(!file.is_open())
        throw FileNotAccessibleException("Could not open file: " + pathAsString, __FILE__, __LINE__);

    /*
        error: expected primary-expression before ‘(’ token
        error: expected type-specifier
    */
    file.imbue(std::locale(file.getloc(), new boost::posix_time::time_facet("%d-%m-%Y %H:%M:%S")));
}

void OutputHandler::log(std::string const& message, WarningLevel const& warningLevel) {
    mutex.lock();
    /*
        error: cannot bind ‘std::basic_ostream<char>’ lvalue to ‘std::basic_ostream<char>&&’
    */
    file << "[" << boost::posix_time::second_clock::universal_time() << "][" << errorPrefixes[warningLevel] << "]: " + message << "\n";
    file.flush();
    mutex.unlock();
}

void OutputHandler::log(std::exception const& exception, WarningLevel const& warningLevel) {
    log(exception.what(), warningLevel);
}

char const * const OutputHandler::errorPrefixes[] = {"NOTICE", "WARNING", "ERROR", "CRASH"};

编译错误:

OutputHandler.cpp: In constructor ‘OutputHandler::OutputHandler(const string&)’:
OutputHandler.cpp:14:24: error: expected primary-expression before ‘(’ token
  file.imbue(std::locale(file.getloc(), new boost::posix_time::time_facet("%d-%m-%Y %H:%M:%S")));
                        ^
OutputHandler.cpp:14:44: error: expected type-specifier
  file.imbue(std::locale(file.getloc(), new boost::posix_time::time_facet("%d-%m-%Y %H:%M:%S")));
                                            ^
OutputHandler.cpp: In member function ‘void OutputHandler::log(const string&, const OutputHandler::WarningLevel&)’:
OutputHandler.cpp:19:7: error: cannot bind ‘std::basic_ostream<char>’ lvalue to ‘std::basic_ostream<char>&&’
  file << "[" << boost::posix_time::second_clock::universal_time() << "][" << errorPrefixes[warningLevel] << "]: " + message << "\n";
       ^
In file included from /usr/include/c++/4.8/istream:39:0,
                 from /usr/include/c++/4.8/fstream:38,
                 from OutputHandler.h:8,
                 from OutputHandler.cpp:7:
/usr/include/c++/4.8/ostream:602:5: error:   initializing argument 1 of ‘std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char; _Traits = std::char_traits<char>; _Tp = boost::posix_time::ptime]’
     operator<<(basic_ostream<_CharT, _Traits>&& __os, const _Tp& __x)
     ^

最佳答案

编译器错误非常神秘,但我认为它想告诉您它不知道类型boost::posix_time::time_facet

尝试将以下行添加到您的 cpp 文件中:

#include <boost/date_time/posix_time/posix_time.hpp>

关于c++ - 错误打印 posix_time,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19212981/

相关文章:

c - 如何给出附加到 shmat() 共享内存段中的起始地址?

c++ - 终止信号或中断,如 Linux 上的 Ctrl+C

c++ - 将 cv::mat 转换为 cv::arr 时出错

c++ - 使用 MPI 在处理器之间分配工作,但所有处理器都在完成整个工作,而不是只做其中的一部分

c++ - boost 反序列化优化?

c++ - 找不到boost库

c++ - 高效读取 3MB 文本文件,包括。解析

c - POSIX fopen(NULL ,"r")

c++ - 在 C++11 标准中,为什么要保留 char 类型实现相关性?

c++ - 为什么不调用覆盖的 `operator new`?