Reputation: 16541
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 :)
Upvotes: 0
Views: 172
Reputation: 10350
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 {
...
}
Upvotes: 1
Reputation: 7613
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.
Upvotes: 2
Reputation: 40859
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.
Upvotes: 2