c++ - Errors Printing posix_time -
i having these weird compile errors when trying compile code.
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 ‘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"};
compile error:
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 ‘std::basic_ostream<char>&&’ file << "[" << boost::posix_time::second_clock::universal_time() << "][" << errorprefixes[warninglevel] << "]: " + message << "\n"; ^ in file included /usr/include/c++/4.8/istream:39:0, /usr/include/c++/4.8/fstream:38, outputhandler.h:8, 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) ^
the compiler error quite cryptic, think wants tell not know type boost::posix_time::time_facet.
try add following line cpp file:
#include <boost/date_time/posix_time/posix_time.hpp>
Comments
Post a Comment