C++ 无法将std::time\u get.get\u date函数作为参数传递给另一个函数
我试图将std::time\u get.get\u date函数作为参数传递给另一个函数 我想这样做的原因是还要将其他std::time_get函数(具有相同签名)传递给另一个函数 但是,我得到了一个编译错误--编辑--在线版本如下:C++ 无法将std::time\u get.get\u date函数作为参数传递给另一个函数,c++,C++,我试图将std::time\u get.get\u date函数作为参数传递给另一个函数 我想这样做的原因是还要将其他std::time_get函数(具有相同签名)传递给另一个函数 但是,我得到了一个编译错误--编辑--在线版本如下: /**将time\u get.get\u date函数成员传递给函数 */ #include///time\u get #include///istreambuf_迭代器 #include///tm #include///ios\u base #包括 使用名称空
/**将time\u get.get\u date函数成员传递给函数
*/
#include///time\u get
#include///istreambuf_迭代器
#include///tm
#include///ios\u base
#包括
使用名称空间std;
使用InIt=istreambuf_迭代器;
使用IB=ios_基;
使用ST=IB::iostate;
使用TG=常数时间;
使用pGet=InIt(TG::*)(InIt,InIt,IB&,ST&,tm*);
///函数在时间上的地址
自动gd=&TG::获取_日期;
///pGet函数参数
初始frm(cin),til;
ST;
tm-t;
语言环境loc;///经典的“C”语言环境
TG&TG=///时间
使用切面(loc);
无效getdtpart(TG&TG,pGet-fget)
{
(tg.*(fget))(frm、til、cin、st和t);
}
int main()
{
getdtpart(tg,gd);
cout似乎问题在于在两个地方使用/不使用const
第一部分是pGet
,它应该捕获get_date
的签名,这是一个const
成员函数。因此在末尾添加const
:
using pGet = InIt (TG::*) (InIt, InIt, IB&, ST&, tm*) const;
另一部分是TG类型中const
的us,这似乎与定义pGet
时使用TG
相冲突
考虑到这一点,并将一些变量移动到main中以避免名称阴影,我将编译此版本:
/** Passing the time_get<char>.get_date function member to a function
*/
#include <locale> /// time_get<char>
#include <iterator> /// istreambuf_iterator<char>
#include <ctime> /// tm
#include <ios> /// ios_base
#include <iostream>
using namespace std;
using InIt = istreambuf_iterator<char>;
using IB = ios_base;
using ST = IB::iostate;
using TG = time_get<char>;
using pGet = InIt (TG::*) (InIt, InIt, IB&, ST&, tm*) const;
/// Addresses of functions in time_get<char>
auto gd = &TG::get_date;
/// pGet function arguments
InIt frm (cin), til;
ST st;
tm t;
void getdtpart(const TG& tg, pGet fget)
{
(tg.*(fget)) (frm, til, cin, st, &t);
}
int main()
{
locale loc; /// classic "C" locale
const TG& tg = /// time_get<char> facet
use_facet<TG> (loc);
getdtpart(tg, gd);
cout << "Completed" << endl;
}
/**将time\u get.get\u date函数成员传递给函数
*/
#include///time\u get
#include///istreambuf_迭代器
#include///tm
#include///ios\u base
#包括
使用名称空间std;
使用InIt=istreambuf_迭代器;
使用IB=ios_基;
使用ST=IB::iostate;
使用TG=时间;
使用pGet=InIt(TG::*)(InIt,InIt,IB&,ST&,tm*)常量;
///函数在时间上的地址
自动gd=&TG::获取_日期;
///pGet函数参数
初始frm(cin),til;
ST;
tm-t;
void getdtpart(常数TG&TG,pGet fget)
{
(tg.*(fget))(frm、til、cin、st和t);
}
int main()
{
locale loc;///经典的“C”语言环境
const TG&TG=///时间
使用切面(loc);
getdtpart(tg,gd);
库特
using pGet = InIt (TG::*) (InIt, InIt, IB&, ST&, tm*) const;
/** Passing the time_get<char>.get_date function member to a function
*/
#include <locale> /// time_get<char>
#include <iterator> /// istreambuf_iterator<char>
#include <ctime> /// tm
#include <ios> /// ios_base
#include <iostream>
using namespace std;
using InIt = istreambuf_iterator<char>;
using IB = ios_base;
using ST = IB::iostate;
using TG = time_get<char>;
using pGet = InIt (TG::*) (InIt, InIt, IB&, ST&, tm*) const;
/// Addresses of functions in time_get<char>
auto gd = &TG::get_date;
/// pGet function arguments
InIt frm (cin), til;
ST st;
tm t;
void getdtpart(const TG& tg, pGet fget)
{
(tg.*(fget)) (frm, til, cin, st, &t);
}
int main()
{
locale loc; /// classic "C" locale
const TG& tg = /// time_get<char> facet
use_facet<TG> (loc);
getdtpart(tg, gd);
cout << "Completed" << endl;
}