2013-10-06 104 views
1

當我試圖編譯此代碼時,出現了這些奇怪的編譯錯誤。錯誤打印posix_time

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) 
    ^

回答

1

的編譯r的錯誤是相當神祕的,但我認爲它想告訴你,它不知道類型boost :: posix_time :: time_facet

嘗試下面的行添加到您的CPP文件:

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