Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在结构上使用模板时遇到问题 我是一个初学编程和C++的初学者。我以前使用过模板,但使用的方式非常有限,我不知道我做错了什么: template <typename TElement> struct list{ // if I try list<TElement> => list is not a template TElement data; struct list *next; } node_type; // also tried node_type<TElement>, other incomprehensible errors node_type *ptr[max], *root[max], *temp[max];_C++_Templates_Struct - Fatal编程技术网

在结构上使用模板时遇到问题 我是一个初学编程和C++的初学者。我以前使用过模板,但使用的方式非常有限,我不知道我做错了什么: template <typename TElement> struct list{ // if I try list<TElement> => list is not a template TElement data; struct list *next; } node_type; // also tried node_type<TElement>, other incomprehensible errors node_type *ptr[max], *root[max], *temp[max];

在结构上使用模板时遇到问题 我是一个初学编程和C++的初学者。我以前使用过模板,但使用的方式非常有限,我不知道我做错了什么: template <typename TElement> struct list{ // if I try list<TElement> => list is not a template TElement data; struct list *next; } node_type; // also tried node_type<TElement>, other incomprehensible errors node_type *ptr[max], *root[max], *temp[max];,c++,templates,struct,C++,Templates,Struct,如果这有任何意义(其中活动是一个类,但可以是任何其他类)。尝试以下方法: template <typename TElement> struct node{ TElement data; node* next; }; node<int>* ptr[max], *root[max], *temp[max]; 模板 结构节点{ 远程通讯数据; 节点*下一步; }; 节点*ptr[max],*root[max],*temp[max];

如果这有任何意义(其中
活动
是一个类,但可以是任何其他类)。

尝试以下方法:

template <typename TElement>
struct node{   
    TElement data;
    node* next;
};    

node<int>* ptr[max], *root[max], *temp[max];
模板
结构节点{
远程通讯数据;
节点*下一步;
};    
节点*ptr[max],*root[max],*temp[max];

一个附加的建议:避免从标准C++库中命名类型之后的自定义类型(如<代码>列表,<代码>向量,<代码>队列……这些都在命名空间<代码> STD<代码>中。它是混乱的,可能会导致名称冲突(除非你把它放在你自己的命名空间中,你需要明确地使用你放在代码>使用命名空间STD;)。< / P> < P>不要尝试通过C++学习错误和猜测语法,读一个.< 因为这是无效的语法,错误是告诉您

list
不是模板,因为在您编写
list
的时候,您还没有完成
list
的声明,因此编译器不知道它是什么,如果列表模板没有定义,您就无法得到某个列表

template <typename TElement>
  struct list{
    TElement data;
    struct list *next;
  }node_type;
但是在你的例子中,
列表
不是一个类型,而是一个模板<代码>列表
是一种类型,但是
列表
本身不是有效的类型,它只是一个模板,当您“填空”时,可以从中创建类型,即提供一个类型来替代参数
远程通讯

看起来您甚至没有试图声明变量,只是盲目地猜测语法

// also tried node_type<TElement>, other incomprehensible errors
节点类型
不是一种类型,因此无法工作。此外,您应该避免养成在一行上声明多个变量的坏习惯。写得更清楚:

int* p1;
int* p2;
而不是

int *p1, *p2;

另外,您确定需要指针数组吗?既然你显然不知道你在做什么,那么使用标准的容器类型来做你的工作就更明智了。

我想你最好是从C++上选一本好书,然后自己努力工作。C++不工作;不清楚你想要实现什么;这不太可能是一种高效的学习方式
但是没有
typedef
。顺便说一句,我发现这个错误完全可以理解。你还想在这里看到什么错误消息?我不知道我希望错误会说什么,但我仍然不知道它会说什么。我也同意这不是学习C++的好方法,但听起来好像是反直觉的。在我的学校学习C++并不重要,因为没有人在教C++。我们被期望知道一些东西并通过考试。。。但这是另一个时间的问题。我需要使用几个类对象(抽象数据类型),它们很快就会成为一个自我实现的字典(家庭作业),所以使用会适得其反,因为我使用模板的唯一原因是使用我想要的任何东西。你可以使用任何类型作为模板参数;我把
int
作为一个例子,但我需要它是远程通讯,如果我尝试
node\u type
,远程通讯“不在此范围内声明”。您想创建自己的列表类型,它可以保存任何类型的数据。这就是您将此结构作为模板的原因:编译器将用提供的实际(任意)类型(在我的示例中为
int
)替换
TElement
,因此编译器将替换为我的类型。我理解,但我不明白为什么我必须使用int而不是我指定的类型,这是非常混乱和违反直觉的(对我来说)。非常感谢。我想你是不会对尝试和错误进行尝试的,但时间不允许我学习C++。我喜欢编程,但我没有足够的精力每4个月学习一门新语言,以通过一些随机考试,但这不是你的问题:P
struct A {
  int i;
} a;        // the variable 'a' is an object of type 'A'
// also tried node_type<TElement>, other incomprehensible errors
node_type *ptr[max], *root[max], *temp[max];
int* p1;
int* p2;
int *p1, *p2;