简体   繁体   English

如何使用typdef模板化的结构指针?

[英]C++ - How to typdef templated struct pointer?

Here is my code: 这是我的代码:

#include<iostream>
using namespace std;

template<typename T>
struct Doublenode{
    T data;
    struct Doublenode<T>* prev;
    struct Doublenode<T>* next;
};
template<typename T>
using Double_node = typename Doublenode <T>::Double_node;
template<typename T>
using DoubleNodePtr = typename Double_node <T>* ::DoubleNodePtr;

This gives me the folowing error: 这给了我以下错误:

error: expected nested-name-specifier using DoubleNodePtr = typename (^)Double_node * ::DoubleNodePtr; 错误:使用DoubleNodePtr的预期嵌套名称说明符=类型名(^)Double_node * :: DoubleNodePtr;

What is the right way to typdef a templated struct pointer? typdef模板化的结构指针的正确方法是什么?

The syntax of a using type alias, be it templated or not, is as follows: using类型别名(无论是否为模板)的语法如下:

using <alias name> = <existing type name>;

You are writing it as 您正在将其编写为

using <alias name> = <existing type name> :: <alias name>;

It could work if <existing type name> has a member type named like <alias name> . 如果<existing type name>具有类似<alias name>的成员类型,则可以使用 But it's not correct in general. 但这通常是不正确的。 You problem, ultimately, seems to be about misunderstanding the required syntax. 最终,您的问题似乎是对所需语法的误解。 Which when applied to your post is simply: 应用于您的帖子时,只需:

template<typename T>
using Double_node = Doublenode<T>;
 template<typename T> using DoubleNodePtr = typename Double_node <T>* ::DoubleNodePtr; 

Pointer types do not have members or a scope, so you cannot apply the scope resolution operator on a pointer. 指针类型没有成员或作用域,因此您不能在指针上应用作用域解析运算符。

 template<typename T> using Double_node = typename Doublenode <T>::Double_node; 

Doublenode doesn't have a member called Double_node , so this can only be sensible if you intend to specialize Doublenode and those specializations have that member. Doublenode没有名为Double_node的成员,因此仅当您打算专门使用Doublenode并且那些专门知识拥有该成员时, Doublenode


C++ - How to typdef templated struct pointer? 如何使用typdef模板化的结构指针?

Like this: 像这样:

template<typename T>
using DoubleNodePtr = Double_node<T>*;

However, it is usually a bad idea to create a type alias for a pointer. 但是,为指针创建类型别名通常不是一个好主意。 Don't do it. 不要这样

template<typename T>
using Double_node = Doublenode<T>;

template<typename T>
using DoubleNodePtr = Double_node<T>*;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM