C++模板问题

发布于 2024-10-20 20:20:27 字数 1076 浏览 2 评论 0原文

我得到了作业,我必须为多维向量编写一个类,

我被告知必须使用模板,这对我来说是全新的,准确地说,那么我必须使用 templatetemplatetemplate code> ,其中 n 定义我的向量有多少个维度。

到目前为止,我有这个,但它不起作用:

vector.h

#include <vector>

template <unsigned short n>
class Vector {
public:
    std::vector<float> coords;

    Vector<n>();

};

vector.cpp

 #include "vector.h"

 Vector<n>() {
 for(int i = 0; i < n; i++) {
     coords.push_back(0.0);
 }
};

我的默认构造函数使向量线为 0.0

我在向量上遇到错误。 cpp C:\CodeBlocks\kool\praks3\vector.cpp|3|错误:'n' 未在此范围内声明|

进一步 ..为什么我必须使用模板在这里,我阅读了模板教程,它用于优化代码并减少代码重复,但是

当我查看此示例时,这里的 n 必须始终为无符号短,我知道我使用模板,因为我的数据类型可以是我想要的任何内容,我可以将它用于整数、双精度数或任何类型的数字,

但目前,当我定义了必须使用哪种数据类型时,使用模板的意义何在?

template <class dataType>
dataType GetMin (dataType a, dataType b) {
return ((a < b) ? a : b );
}

如果有什么不清楚的,请随时向我询问! 因为有很多事情让我感到困惑:)

i got assignment, where i must write a class for multidimensional vector

i was told that i must use templates, which is totally new to me, and to be exact, then i must use template <unsigned short n> , where n defines how many dimensions my vector has.

so far i have this, but it is not working:

vector.h

#include <vector>

template <unsigned short n>
class Vector {
public:
    std::vector<float> coords;

    Vector<n>();

};

vector.cpp

 #include "vector.h"

 Vector<n>() {
 for(int i = 0; i < n; i++) {
     coords.push_back(0.0);
 }
};

my default constructor makes vector cords to 0.0

i get error on vector. cpp C:\CodeBlocks\kool\praks3\vector.cpp|3|error: 'n' was not declared in this scope|

furthermore ..why i must use template here, i read the template tutorial and it is used to optimize code and reduce code repeation, but here the n must be always unsigned short

when i look this exmaple, i understand that i use template, because my datatype can be anything i want, and i can use it for ints,doubles or any kind of numbers

but at the moment what is the point of using template, when i have defined which datatype i must use ???

template <class dataType>
dataType GetMin (dataType a, dataType b) {
return ((a < b) ? a : b );
}

if something is unclear, feel free to ask from me!
because there's alot thats confused for me :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

莫相离 2024-10-27 20:20:27

您确实应该首先阅读模板: http://www.cplusplus.com/doc/tutorial /templates/

模板很好,因为它们使您能够使用泛型类型。例如,使用相同的代码获取两个对象(int、long、float 等)的最大值或最小值。在你的情况下,使用模板是一个好主意,因为它使你能够拥有整数、浮点数、双精度数或任何你喜欢的向量。

另一件事是,在使用模板时,将标头和实现保留在同一文件(标头)中是最简单的。

您的构造函数 Vector(); 的表述不正确。因为您的类前面有 template,所以您在其中编写的所有内容都将使用该模板。

template <unsigned short n>
class Vector {
public:
  // Here..    
};

因此,只需像通常那样编写构造函数即可。

You should really read up on templates first: http://www.cplusplus.com/doc/tutorial/templates/

Templates are nice because they enable you to work with generic types. For instance, getting the maximum or minimum of two objects (int, long, float etc.) using the same code. In your case it's a good idea to use templates because it enables you to have vectors of ints, floats, doubles or whatever you like.

Another thing, it's easiest to keep header and implementation in the same file (the header) when using templates.

Your constructor, Vector<n>();, is incorrectly stated. Because your class has template <unsigned short n> before it, everything you write inside it will be with that template.

template <unsigned short n>
class Vector {
public:
  // Here..    
};

So just write your constructor as you would normally do there.

满天都是小星星 2024-10-27 20:20:27

您可能被告知在这里使用模板,不是因为这是正确的做法,而是因为这样做应该可以教会您如何使用它们。具体来说,讲师试图向您展示模板可以采用某些非类型参数。

不过,您的问题与模板关系不大,更多的是与不了解如何构建 C++ 类有关。当您在类中定义函数时,您需要使用作用域语法:

// non-templated vector
Vector::Vector()
{
  ...
}

这是问题 1。问题 2 是您尝试在 cpp 文件中定义模板功能。你不能这样做。您必须将函数定义放在标题中(除了少数情况,我稍后会让您的老师回顾一下)。

接下来您需要了解的是 std::vector 已经有一个可以使用的质量初始化器来代替 for 循环:

std::vector<float> my_vect(42, 0.0); // builds a vector with 42 floats all initialized to 0.0

使用这些线索您应该能够解决您的家庭作业问题。

You're probably being told to use a template here not because it's the right thing to do, but because doing so should hopefully teach you how to use them. Specifically the instructor is trying to show you that templates can take certain non-type parameters.

Your problems though have very little to do with templates and more to do with not understanding how to build C++ classes. When you are defining a function within a class you need to use scope syntax:

// non-templated vector
Vector::Vector()
{
  ...
}

That's problem 1. Problem 2 is that you're trying to define template functionality in a cpp file. You can't do this. You must put the function definitions in the header (minus a few cases I'll let your teacher go over later).

The next thing you need to understand is that std::vector already has a mass-initializer you can use instead of a for loop:

std::vector<float> my_vect(42, 0.0); // builds a vector with 42 floats all initialized to 0.0

Using those clues you should be able to solve your homework problem.

脱离于你 2024-10-27 20:20:27

回答:当我定义了必须使用哪种数据类型时,使用模板的意义是什么???

你的类在编写时没有任何意义。我希望看到要存储的类型和(可能)大小作为模板参数。

template <class T, unsigned short n>
class MyVector {
...
}

To answer: what is the point of using template, when i have defined which datatype i must use ???

There is no point with your class as it is written. I would have expected to see both the type to be stored AND (possibly) the size as template parameters.

template <class T, unsigned short n>
class MyVector {
...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文