无法将std :: time_get.get_date函数作为参数传递给另一个函数

时间:2018-01-27 16:09:33

标签: c++

我试图将std :: time_get.get_date函数作为参数传递给另一个函数。

我想这样做的原因是还将其他std :: time_get函数(具有相同签名)传递给另一个函数。

但是,我收到编译错误。 --EDIT - 在线版本如下:
http://coliru.stacked-crooked.com/a/8f8df97ff106e9be

/** 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 = const time_get<char>;
using pGet = InIt (TG::*) (InIt, InIt, IB&, ST&, tm*);

/// Addresses of functions in time_get<char>
auto gd = &TG::get_date;

/// pGet function arguments
InIt frm (cin), til;
ST st;
tm t;

locale loc;                     /// classic "C" locale

TG& tg =                        /// time_get<char> facet
    use_facet<TG> (loc);


void getdtpart(TG& tg, pGet fget)
{
    (tg.*(fget)) (frm, til, cin, st, &t);
}


int main()
{
    getdtpart(tg, gd);

    cout << "Completed" << endl;
}

编译错误是:

main.cpp: In function 'void getdtpart(TG&, pGet)':

main.cpp:34:40: error: invalid conversion from 'TG* {aka const std::__cxx11::time_get<char>*}' to 'TG* {aka std::__cxx11::time_get<char>*}' [-fpermissive]

     (tg.*(fget)) (frm, til, cin, st, &t);
                                        ^

main.cpp: In function 'int main()':
main.cpp:40:21: error: cannot convert 'std::istreambuf_iterator<char, std::char_traits<char> > (std::__cxx11::time_get<char>::*)(std::istreambuf_iterator<char, std::char_traits<char> >, std::istreambuf_iterator<char, std::char_traits<char> >, std::ios_base&, std::_Ios_Iostate&, tm*) const' to 'pGet {aka std::istreambuf_iterator<char, std::char_traits<char> > (std::__cxx11::time_get<char>::*)(std::istreambuf_iterator<char, std::char_traits<char> >, std::istreambuf_iterator<char, std::char_traits<char> >, std::ios_base&, std::_Ios_Iostate&, tm*)}' for argument '2' to 'void getdtpart(TG&, pGet)'

     getdtpart(tg, gd);
                     ^

很明显这是因为'const'的存在与否,但我不知道如何解决这个问题。我怎么能?

1 个答案:

答案 0 :(得分:1)

似乎问题是pGet在两个地方的使用/不使用。

第一部分是get_date,它应该捕获const const成员函数的签名。所以最后添加using pGet = InIt (TG::*) (InIt, InIt, IB&, ST&, tm*) const;

const

另一部分是TG类型中TG的我们,这似乎与定义pGet时使用/** 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; } 冲突。

考虑到这一点,并将一些变量移动到main以避免名称阴影,我得到这个版本进行编译:

db.getCollection('_survey.response').aggregate([
    {
        $match:
        {
            $and:[
                {
                    "Provider.Name": {
                    $in:[
                            "Comcast",
                            "AT&T"
                        ]
                    }
                },
                {
                    CreatedOn: {
                        $lt: ISODate('2017-05-10'),
                        $gt: ISODate('2017-01-01')
                    }
                }
            ]
        }
    },
    {
        $group:{
            _id: {
                Status : "$Status",
                Survey: "$SurveyName"
            },
            SurveyId: {$addToSet: "$SurveyId"},
            Count: {$sum: 1}
        }
    },
    {
        $group:{
            _id: {
                SurveyName: "$_id.Survey"
            },
            Status: {$push: "$_id.Status"},
            Count : { $push : "$Count" } 
        }
    },
    {
        $project:{
            _id: 0,
            SurveyId: "$_id.SurveyId",
            Survey: "$_id.SurveyName",
            Status: 1,
            Count: 1
        }
    }
]);