c ++:我如何调用一个类中定义的朋友模板函数? [英] c++: how do i call a friend template function defined inside a class?

查看:111
本文介绍了c ++:我如何调用一个类中定义的朋友模板函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi guys请帮我这个函数
i从我的书中得到了这个例子,但我不知道如何实际调用ticket函数
这是代码:

hi guys please help me with this function i got this example from my book, but i have no idea how to actually call the ticket function this is the code:

#include <iostream>
    class Manager { 
    public:
        template<typename T> 
            friend int ticket() { 
                return ++Manager::counter; 
            } 
        static int counter; 
    }; 

int main()
{
    Manager m;
    std::cout << "ticket: " << ticket<int>() << std::endl;
}

我得到候选函数不可访问错误msg
非常感谢您的查找!

i get the "candidate function(s) not accessible" error msg thanks alot for looking!

推荐答案

#include <iostream>

class Manager {
public:
    template<typename T>
        friend int ticket() {
            return ++Manager::counter;
        }
    static int counter;
};

int Manager::counter;

int main()
{
    std::cout << "ticket: " << ticket<int>() << std::endl;
    std::cout << "ticket: " << ticket<int>() << std::endl;
}

您使用什么编译器?

这篇关于c ++:我如何调用一个类中定义的朋友模板函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆