重载运算符T ***()

时间:2015-01-08 16:34:41

标签: c++ struct operator-overloading

我不知道,为什么我的代码不起作用。我创建struct和内置重载运算符operator T***(),主要是我喜欢使用以下符号const int * const * const * p1 = a;

struct A{
    template<class T> operator T***(){}
};

int main(){
    A a;
    const int * const * const * p1 = a;
    return 0;
}

Error: undefined reference to '_ZN1AcvPPPT_IKiEEv'

3 个答案:

答案 0 :(得分:3)

您只是错过了为您的类型转换功能提供实现

struct A{
    template<class T> operator T***() {
        return nullptr; // Do whatever you want to do here.
    }
};

请见working sample

答案 1 :(得分:2)

我完全不知道你要做什么,但你的问题是链接器问题。您只需声明(但不定义)转换运算符T***。你必须定义它,

template<class T> operator T***()
{ 
   // define it here
}

答案 2 :(得分:0)

在编辑问题之前:

template<class T> operator T***();

声明操作员模板,但没有定义它,因此它未定义的错误。即使您在另一个源文件中定义模板,您也会收到该错误,因为必须在每个使用它们的翻译单元中定义模板。

编辑后:

template<class T> operator T***(){}

代码编译,但由于操作符缺少return语句,因此具有未定义的行为。