重载运算符 T***()

Overloading operator T***()

我不知道,为什么我的代码不起作用。我创建 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'

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

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

你只是错过了给你的类型转换函数一个实现

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

请看working sample

在您编辑问题之前:

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

声明了运算符模板,但没有定义它,因此出现未定义的错误。即使您在另一个源文件中定义模板,您也会遇到该错误,因为模板必须在使用它们的每个翻译单元中定义。

编辑后:

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

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