
Ubuntu日本語フォーラム

ログインしていません。
Ubuntuのバージョン:Ubuntu 18.04.2 LTS
ソースファイル
hoge@hoge-VirtualBox:~/cTest$ cat hellow.cpp
#include <iostream>
#include <stdio.h>
#define STR(var) #var
enum LogLevel
{
ERROR,
INFO,
DEBUG
};
std::ostream& logStream(LogLevel level);
std::ostream& logStream(LogLevel level) {
std::ostream stream = std::cout;
if( level == ERROR ) {
stream = std::cerr;
}
char szTime[90] = { 0 };
time_t now = time(NULL);
struct tm *pnow = localtime(&now);
sprintf(szTime, "%04d/%02d/%02d %02d:%02d:%02d", pnow->tm_year + 1900, pnow->tm_mon + 1, pnow->tm_mday,
pnow->tm_hour, pnow->tm_min, pnow->tm_sec);
stream << szTime << "[" << STR(lebel) << "]";
return stream;
}
int main() {
logStream(INFO) << "Hellow." << std::endl;
return 0;
}コンパイル結果
hoge@hoge-VirtualBox:~/cTest$ c++ hellow.cpp
hellow.cpp: In function ‘std::ostream& logStream(LogLevel)’:
hellow.cpp:14:29: error: ‘std::basic_ostream<_CharT, _Traits>::basic_ostream(const std::basic_ostream<_CharT, _Traits>&) [with _CharT = char; _Traits = std::char_traits<char>]’ is protected within this context
std::ostream stream = std::cout;
^~~~
In file included from /usr/include/c++/7/iostream:39:0,
from hellow.cpp:1:
/usr/include/c++/7/ostream:391:7: note: declared protected here
basic_ostream(const basic_ostream&) = delete;
^~~~~~~~~~~~~
hellow.cpp:14:29: error: use of deleted function ‘std::basic_ostream<_CharT, _Traits>::basic_ostream(const std::basic_ostream<_CharT, _Traits>&) [with _CharT = char; _Traits = std::char_traits<char>]’
std::ostream stream = std::cout;
^~~~
In file included from /usr/include/c++/7/iostream:39:0,
from hellow.cpp:1:
/usr/include/c++/7/ostream:391:7: note: declared here
basic_ostream(const basic_ostream&) = delete;
^~~~~~~~~~~~~
hellow.cpp:16:17: error: ‘std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator=(const std::basic_ostream<_CharT, _Traits>&) [with _CharT = char; _Traits = std::char_traits<char>]’ is protected within this context
stream = std::cerr;
^~~~
In file included from /usr/include/c++/7/iostream:39:0,
from hellow.cpp:1:
/usr/include/c++/7/ostream:399:22: note: declared protected here
basic_ostream& operator=(const basic_ostream&) = delete;
^~~~~~~~
hellow.cpp:16:17: error: use of deleted function ‘std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator=(const std::basic_ostream<_CharT, _Traits>&) [with _CharT = char; _Traits = std::char_traits<char>]’
stream = std::cerr;
^~~~
In file included from /usr/include/c++/7/iostream:39:0,
from hellow.cpp:1:
/usr/include/c++/7/ostream:399:22: note: declared here
basic_ostream& operator=(const basic_ostream&) = delete;
^~~~~~~~
hellow.cpp:14:15: warning: reference to local variable ‘stream’ returned [-Wreturn-local-addr]
std::ostream stream = std::cout;
^~~~~~
hoge@hoge-VirtualBox:~/cTest$オフライン
c++ は不勉強なので的を外しているかもしれませんが、以下 URL が回答になるのではないでしょうか。
std::cout のみならず std::cerr を代入する箇所もエラーになっているみたいですし。
13.2 ストリームオブジェクトのコピーと割り当て
オフライン
ありがとうございます。
教えていただいたページを参考に修正したところうまくいきました。
coutのアドレスを参照すればよかったのですね。
コンパイル・実行できたソースです。
hoge@hoge-VirtualBox:~/cTest$ cat hellow.cpp
#include <iostream>
#include <stdio.h>
#define STR(var) #var
enum LogLevel
{
ERROR,
INFO,
DEBUG
};
std::ostream& logStream(LogLevel level);
std::ostream& logStream(LogLevel level) {
std::ostream *stream = &std::cout;
const char *szLevel = STR(INFO);
if( level == ERROR ) {
stream = &std::cerr;
szLevel = STR(ERROR);
} else if( level == DEBUG ) {
szLevel = STR(DEBUG);
}
char szTime[90] = { 0 };
time_t now = time(NULL);
struct tm *pnow = localtime(&now);
sprintf(szTime, "%04d/%02d/%02d %02d:%02d:%02d", pnow->tm_year + 1900, pnow->tm_mon + 1, pnow->tm_mday,
pnow->tm_hour, pnow->tm_min, pnow->tm_sec);
*stream << szTime << " [" << szLevel << "] ";
return *stream;
}
int main() {
logStream(INFO) << "Hellow." << std::endl;
return 0;
}オフライン