Sam*_*nha 3 c++ struct typedef typename
我正在尝试在 C++ 中对具有类型名的结构进行前向声明。像这样的事情是完全有效的:
typedef struct foo foo;
struct foo{
int f;
};
Run Code Online (Sandbox Code Playgroud)
我的结构只是有一个类型名,所以我尝试了这个:
template <typename T>
typedef struct mV<T> mV;
template <typename T>
struct mV{
//contents of struct
};
Run Code Online (Sandbox Code Playgroud)
但是,我随后收到错误a typedef cannot be a template、explicit specialization of undeclared template struct和redefinition of 'mV' as different kind of symbol。我该如何解决这个问题?
您正在描述前瞻性声明。(Afuture在现代 C++ 中是完全不同的东西)。
typedef在 C++ 中,不需要也很少需要别名结构标记。相反,您只需声明类类型并完成它即可。
// typedef struct mV mV; // not this
struct mV; // instead this
Run Code Online (Sandbox Code Playgroud)
模板也是如此
template<class T>
struct mV;
Run Code Online (Sandbox Code Playgroud)
如果您需要/想要为模板类型附加别名,您仍然可以通过using
template<class T>
struct mV;
template<class T>
using MyAliasNameHere = mV<T>;
Run Code Online (Sandbox Code Playgroud)
掌握了所有这些,并阻止我猜测您很快就会发现的内容,您可能还需要阅读以下内容:为什么模板只能在头文件中实现?。有件事告诉我,这将变得高度相关。
| 归档时间: |
|
| 查看次数: |
250 次 |
| 最近记录: |