No, you did not implement the default constructor and copy constructor for project::type. Frankly, you do not even need to declare the copy constructor, copy assignment operator and destructor for project::type.Originally Posted by -EquinoX-
Furthermore, note that names that begin with an underscore followed by an uppercase letter (or that contain consecutive underscores) are reserved to the implementation (i.e., compiler and standard library) for any use. As such, _VECTOR_H is not a good choice for a header inclusion guard, and you should use T instead of _T for the template parameter. Additionally, use T instead of type for the project::vector template parameter; type is too confusing, especially when you have a class template named type.
As in you have checked with your instructor? After all, int is a type, so you do not need type<int> unless there is some special reason.Originally Posted by -EquinoX-
That can be interpreted as a constraint on the types that project::vector can be instantiated with.Originally Posted by -EquinoX-
Last edited by laserlight; 11-16-2009 at 10:38 PM.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
how can this be done? mind giving an example...
all he's said to me is:
vector is made of up of type objects, so that constraint belongs only to the template parameter used for your class. I am guessing you're right, but not quite clear by constraint belong to the template parameter
Last edited by -EquinoX-; 11-16-2009 at 10:46 PM.
Consider this function template:Originally Posted by -EquinoX-
Objects of type T must be copyable. This does not mean that you have to create a class template so that you have objects that are copyable, rather, it means that you can only use this swap function template with objects of built-in or pointer types, or of a class type with a public copy constructor and public copy assignment operator.Code:template<typename T> void swap(T& x, T& y) { T t(x); x = y; y = t; }
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
so what would I need to change in my original class besides of erasing the type class?
also it takes an int... what is the int for
Last edited by -EquinoX-; 11-16-2009 at 11:24 PM.
Well, if I were you, I would store a std::vector<T> instead of a T*, and thus get rid of the copy constructor and destructor and the compiler supplied ones would then suffice. Actually, in that case csize and size would also become unnecessary.Originally Posted by -EquinoX-
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I am lost to what you're saying there.... store a std::vector<T> inside my templatized vector class..
Before I give an example... what exactly does this mean?Originally Posted by -EquinoX-
So, the vectors should have a fixed length of n?Your vectors should have elements of type, with fixed length n.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
yes that is true... it should have a fixed size and that is done by using the template as I have specified in my code post
I think I understand what you mean now, you're asking me to store my vector using the std::vector, that makes sense... however, still, how do I enforce this one
Elements must be able to take as their optional constructor argument an int.
Is this basically saying that if my vector stores an int then the int can have an argument int? makes no sense at all to me.... 1(2) ???
Actually, since the maximum length is fixed, you could use a std::tr1::array instead, or:Originally Posted by -EquinoX-
Code:#include <cstddef> #include <stdexcept> template<typename T, std::size_t N> class vector { public: vector() : size(0) {} void add(const T& value); vector& operator+=(const vector& other); vector& operator-=(const vector& other); vector operator-() const; T& operator[](std::size_t index); const T& operator[](std::size_t index) const; std::size_t length() const; private: T elements[N]; std::size_t size; }; template<typename T, std::size_t N> void vector<T, N>::add(const T& value) { if (size == N) { throw std::length_error("Maximum size of vector exceeded!"); } elements[size++] = value; }What it means is that you should be able to use integer values to initialise the elements. This would be the case for the built-in types, and also certain class types, e.g., std::complex<double>.Originally Posted by -EquinoX-
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
What it means is that you should be able to use integer values to initialise the elements
Please can you give me an example of this... I don't quite get it...
I am partially guessing because you did not give the full requirements. Basically, I believe that your instructions are to create a vector type that has elements of types that behave like the integer types. I cannot show you a good example because I am not sure what this vector class is really supposed to do. If you want a direct example:Originally Posted by -EquinoX-
That is what I mean by using an integer value to initialise x. Or more generically:Code:int x = 0;
or if we use direct initialisation:Code:T x = 0;
What's T? It could be an int, a long, a double, a std::complex<double> (for the direct initialisation example), my own Integer type, etc.Code:T x(0);
Last edited by laserlight; 11-17-2009 at 12:23 AM.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I guess you're right, but how is this used in the vector class? so say I have a vector called a and I want to add an int with a value 2.2... how would I do it?
You already did it in your initial version, and my example in post #26 is just a variant of yours. What are the += and -= operators supposed to do?Originally Posted by -EquinoX-
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)