矢量模板类型 [英] Vector Template of type Class

查看:89
本文介绍了矢量模板类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何编写具有三个typename变量的模板,以便从main我可以将该模板用作向量,添加并稍后返回或向前继续向量,然后在该位置查看向量的内容?


这里我想出了什么

How do I write a template with three typename variables, so that from main I can use that template as a vector, to add and later go back or go forward in the vector and then view the content of the vector in that location?

Here what I''ve came up with

展开 | < span class =codeLinkonclick =selectAll(this);>选择 | 换行 | 行号

推荐答案

模板是一种模式。你不能有一个模式的矢量。您只能拥有一个对象矢量。要获取对象,您需要指定模板的类型。
A template is a pattern. You cannot have a vector of patterns. You can only have a vector of objects. To get objects, you need tpo specify the types for your template.
展开 | 选择 | Wrap | 行号


感谢您的回复,


我现在修改了它,但它仍然出错。我还没有能够写回到向量中的函数。这是我的代码:
thanks for your reply,

I have modified it now, but it still gets an error. I haven''t been able to write the function to go back in the vector. Here is my code:
展开 | 选择 | Wrap | 行号


首先,你的矢量应该在main()内,而不是在外面。避免使用全局变量。

其次,你在main()中编写了两个函数。你不能这样做。你可以从main()内部调用函数,但是你可以在main之外编写它们。

第三,用作函数参数的模板不能是AClass。记住,AClass是模式。它不是一种类型。在专门化模板之前,它不会是一种类型。函数参数需要是AClass< int,int,int>& ;.

第四,你在main()中有一个没有参数的push_back()。

push_back()总是需要你推入向量的东西。


除此之外,你的代码编译好了。
First, your vector should be inside main() and not outside. Avoid using global variables.
Second, you have writtwn two functions inside main(). You can''t do that. You call the functions from inside main() but you code them outside of main.
Third, a template used as a function argument cannot be AClass. Remember AClass is a pattern. It is not a type. It won''t be a type until you specialize the template. The function argument needs to be AClass<int, int, int>&.
Fourth, you have a push_back() in main() that has no arguments.
push_back() always requires the thing you are pushing into the vector.

Other than that, you code compiles OK.


这篇关于矢量模板类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆