1

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

输出处理程序.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[];
};

输出处理程序.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)
     ^
4

1 回答 1

1

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

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

#include <boost/date_time/posix_time/posix_time.hpp>
于 2014-04-04T07:58:35.613 回答