gpt4 book ai didi

c++ - 语言环境格式化时间输出的实现

转载 作者:行者123 更新时间:2023-11-28 07:11:24 25 4
gpt4 key购买 nike

我正在尝试在程序中实现 How to get current time and date in C++? 中给出的 C++ 打印语言环境时间的代码.

我做了一些更正以使代码适应我的标题,但它仍然给我一个错误“无法解析方法‘put’”。我是新手,所以我找不到答案。我在 Eclipse 上。我已将编辑器的可扩展性编号设置为 50000。此外,该文件在包含和代码之间还有另一个类。

代码是这样的:

#ifndef LISTA_H_
#define LISTA_H_

#include "nodel.h"
#include "hntable.h"

#include <iostream>
#include <string>
#include <iterator>
#include <time.h>

using namespace std;

class timefmt
{
public:
timefmt(std::string fmt)
: format(fmt) { }

friend ostream& operator <<(ostream &, timefmt const &);

private:
string format;
};


std::ostream& operator <<(std::ostream& os, timefmt const& mt)
{
std::ostream::sentry s(os);

if (s)
{
time_t t = time(0);
tm const* tm = localtime(&t);
ostreambuf_iterator<char> out(os);

use_facet<time_put<char>>(os.getloc())
.put(out, os, os.fill(),
tm, &mt.format[0], &mt.format[0] + mt.format.size());
}

os.width(0);

return os;
}

#endif /* LISTA_H_ */

编辑:如果我包含“locale”,之前显示的错误就解决了,但是:`operator<<(std::ostream&, timefmt const&)' 的多重定义。对此有什么提示吗?

最佳答案

只需添加 #include <locale> , 和你的代码 compiles

关于c++ - 语言环境格式化时间输出的实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20916958/

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