【问题标题】:passing a dummy int int the template argument [duplicate]传递一个虚拟int int模板参数[重复]
【发布时间】:2015-06-04 14:09:49
【问题描述】:

最近我卡在模板中,我在模板中传递了一个虚拟参数,但这不起作用,并给出编译错误,这是我的代码..

#include <iostream>
using namespace std;
template<typename T>
class A{
    private:
        T b;
    public:
        A()
        {
            cout<<"1st is executing "<<endl;
        }
};
template<typename T,int>
class A{
    private:
        T b;
    public:
        A(){
            cout<<"2nd is executing "<<endl;
        }
};
int main(){
    A<string> a;
    A<string,100> b;
}

在我看来它应该可以正常工作,但它给出了重新声明错误,我不知道为什么.....请帮助谢谢

【问题讨论】:

    标签: c++


    【解决方案1】:

    我不完全确定,但我认为是template &lt;typename T = int&gt;

    【讨论】:

    • 这是默认参数,它被数据类型 i-e int 或 char 替换,但在我的情况下,如果它是 int ...,它将被 10 等 const 值替换。
    猜你喜欢
    • 2018-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多