gpt4 book ai didi

c++ - 错误打印 posix_time

转载 作者:太空宇宙 更新时间:2023-11-04 11:48:19 27 4
gpt4 key购买 nike

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

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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com