C++中带有模板参数的Typedef

编程入门 行业动态 更新时间:2024-10-15 02:33:44
本文介绍了C++中带有模板参数的Typedef的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我该如何解决这个错误?

How can I solve this error?

我的头文件

template<typename T>
class C1 {
public:
typedef std::vector<T::F> TFV;
TFV Function1();
};

我的 CPP 文件

template<typename T>
TFV C1::Function() //error: ‘TFV’ does not name a type
{ }

推荐答案

首先,使用 typename 关键字告诉编译器将 F 解释为 (qualified) 类型名称:

First of all, use the typename keyword to tell the compiler to interpret F as the (qualified) name of a type:

typedef std::vector<typename T::F> TFV;
//                  ^^^^^^^^

其次,TFV 不是在全局命名空间中定义的类型,因此您必须在 Function1() 的定义中正确地限定它:

Secondly, TFV is not a type defined in the global namespace, so you have to properly qualify that as well in the definition of Function1():

    template<typename T>
    typename C1<T>::TFV C1<T>::Function1()
//  ^^^^^^^^ ^^^^^^^      ^^^
    { }

最后,类模板的成员函数的定义应该放在头文件中,除非您为所有模板实例提供显式实例,否则会隐式生成.

Finally, the definition of member functions of a class template should be placed in a header file, unless you are providing explicit instantiations for all the template instantiations you would otherwise implicitly generate.

如果不这样做,很可能会导致链接器出现未解决的引用错误.

Failing to do so will most likely result in an unresolved references error from the linker.

这篇关于C++中带有模板参数的Typedef的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-05-01 06:17:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1405471.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模板   参数   Typedef

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!