Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/147.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 模板构造函数重载问题_C++_Templates_Function Templates_Constructor Overloading - Fatal编程技术网

C++ 模板构造函数重载问题

C++ 模板构造函数重载问题,c++,templates,function-templates,constructor-overloading,C++,Templates,Function Templates,Constructor Overloading,我有一个包含三个构造函数的类模板,其中一个是函数模板 template<class T> class TemplateOverLoading { public: TemplateOverLoading(void){}; ~TemplateOverLoading(void){}; //constructor that take reference TemplateOverLoading(std::string& qName, T& qVa

我有一个包含三个构造函数的类模板,其中一个是函数模板

template<class T>
class TemplateOverLoading
{
public:
    TemplateOverLoading(void){};
    ~TemplateOverLoading(void){};
    //constructor that take reference
    TemplateOverLoading(std::string& qName, T& qValue )
        :   mName(qName),
            mValue( &qValue)
    {
        std::cout << "Reference -> "<< *mValue <<"\n";
    }

    //Template constructor that takes array
    template<class T, int N>
    TemplateOverLoading(std::string& qName, T (&t)[N])
        :   mName(qName),
            mValue(t)
    {
        std::cout << "Array ->\n";
        for(int i = 0; i < N; i++)
            std::cout<< mValue[i];
        std::cout << std::endl;
    }

    //Other constructor that take pointer
    TemplateOverLoading(std::string& qName, T* qValue )
        :   mName(qName),
            mValue( qValue)
    {
        std::cout << "Pointer "<< *mValue <<"\n";
    }
private:
    T*               mValue;
    //T*              mValueArray;
    std::string&    mName;
};
因此,从语法上讲,推导数组大小N是可能的,也是正确的

显然,当存在数组构造函数时,我会混淆编译器。因此,我没有让编译器自动推断,而是尝试为数组构造函数指定模板参数,以仅查找与常规函数不同的内容,模板参数无法具体指定

我曾想过在数组构造函数中引入一个伪参数来区分重载,但这似乎并不好


还有其他办法解决这个问题吗?任何线索我都感激

将指针构造函数参数设置为
T*&qValue

TemplateOverLoading(std::string& qName, T*& qValue )
    :   mName(qName),
        mValue( qValue)
{
    std::cout << "Pointer "<< *mValue <<"\n";
}
这需要更改为非
T类
,例如
U类

template<class U, int N>
        TemplateOverLoading(std::string& qName, U (&t)[N])
模板
模板重载(std::string&qName,U(&t)[N])

您的构造函数还接受一个非常量左值引用,非常量引用不能绑定到临时变量,您正在构造函数调用中传递临时变量,即
std::string(“mIntArray”)
。您需要将其更改为
const std::string&
或按值获取。还有您的成员
std::string&mName
是一个引用,您应该删除那里的
&

将指针构造函数参数设置为
T*&qValue

TemplateOverLoading(std::string& qName, T*& qValue )
    :   mName(qName),
        mValue( qValue)
{
    std::cout << "Pointer "<< *mValue <<"\n";
}
这需要更改为非
T类
,例如
U类

template<class U, int N>
        TemplateOverLoading(std::string& qName, U (&t)[N])
模板
模板重载(std::string&qName,U(&t)[N])

您的构造函数还接受一个非常量左值引用,非常量引用不能绑定到临时变量,您正在构造函数调用中传递临时变量,即
std::string(“mIntArray”)
。您需要将其更改为
const std::string&
或按值获取。还有您的成员
std::string&mName是一个引用,您应该删除那里的
&

@Xymostech:非模板总是比模板更好的匹配,但是如果您将它们都制作为模板,那么将选择更好的匹配构造函数()。转换为“@Jesse Good”,但它给了我编译错误:错误C2668:“TemplateOverLoading::TemplateOverLoading”:使用[T=int]TemplateOverLoading对重载函数的调用不明确。h(32):可以是带有[T=int,U=int]的“TemplateOverLoading::TemplateOverLoading(std::string&,U*)”或“TemplateOverLoading::TemplateOverLoading”(std::string&,T(&)[10]),带[T=int]@Jesse Good似乎不同的编译器对此有不同的解释。我使用的是VS2008,我先前粘贴的代码可以编译。但是,我已经复制粘贴了与您在liveworkspace中放置的相同的代码,它仍然给出编译错误2668->对重载的不明确调用function@jazaman:不,你的编译器是对的,我使用的编译器一个错误。代码不明确。将
U*qValue
更改为
const U*qValue
是解决问题的一种方法(但这样您只能通过构造函数传递
const int*
).@jazaman:我想到了一个好的解决方案,请看我更新的答案。@Xymostech:非模板总是比模板更好的匹配,但是如果您将它们都制作成模板,那么将选择更好的匹配构造函数()。转换为“@Jesse Good”,但它给了我编译错误:错误C2668:“TemplateOverLoading::TemplateOverLoading”:使用[T=int]TemplateOverLoading对重载函数的调用不明确。h(32):可以是带有[T=int,U=int]的“TemplateOverLoading::TemplateOverLoading(std::string&,U*)”或“TemplateOverLoading::TemplateOverLoading”(std::string&,T(&)[10]),带[T=int]@Jesse Good似乎不同的编译器对此有不同的解释。我使用的是VS2008,我先前粘贴的代码可以编译。但是,我已经复制粘贴了与您在liveworkspace中放置的相同的代码,它仍然给出编译错误2668->对重载的不明确调用function@jazaman:不,你的编译器是对的,我使用的编译器一个错误。代码是不明确的。将
U*qValue
更改为
const U*qValue
是解决问题的一种方法(但是你只能通过构造函数传递
const int*
。@jazaman:我想到了一个好的解决方案,请参阅我更新的答案。
template<class U, int N>
        TemplateOverLoading(std::string& qName, U (&t)[N])