templatized class inside templatized class

This is a discussion on templatized class inside templatized class within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by m37h0d reading your assignment, it is my interpretation that your professor did not intend for you to ...

  1. #16
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    Quote Originally Posted by m37h0d View Post
    reading your assignment, it is my interpretation that your professor did not intend for you to implement a separate class called type.
    then how would you satisfy this if you don't have a class

    Elements must be able to take as their optional constructor argument an int.

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    Quote Originally Posted by -EquinoX-
    I have the constructor and everything
    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.

    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.

    Quote Originally Posted by -EquinoX-
    yes I've confirmed that I need to create a class called type as I need to create a templatized class of vector that stores a 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.

    Quote Originally Posted by -EquinoX-
    Elements must be able to take as their optional constructor argument an int.
    That can be interpreted as a constraint on the types that project::vector can be instantiated with.
    Last edited by laserlight; 11-16-2009 at 10:38 PM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #18
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    Quote Originally Posted by laserlight View Post
    That can be interpreted as a constraint on the types that project::vector can be instantiated with.
    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.

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    Quote Originally Posted by -EquinoX-
    how can this be done? mind giving an example...
    Consider this function template:
    Code:
    template<typename T>
    void swap(T& x, T& y)
    {
        T t(x);
        x = y;
        y = t;
    }
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #20
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    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.

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    Quote Originally Posted by -EquinoX-
    so what would I need to change in my original class besides of erasing the type class?
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #22
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    I am lost to what you're saying there.... store a std::vector<T> inside my templatized vector class..

  8. #23
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    Quote Originally Posted by -EquinoX-
    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?
    Your vectors should have elements of type, with fixed length n.
    So, the vectors should have a fixed length of n?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #24
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    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

  10. #25
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    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) ???

  11. #26
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    Quote Originally Posted by -EquinoX-
    I think I understand what you mean now, you're asking me to store my vector using the std::vector, that makes sense...
    Actually, since the maximum length is fixed, you could use a std::tr1::array instead, or:
    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;
    }
    Quote Originally Posted by -EquinoX-
    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) ???
    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>.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #27
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    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...

  13. #28
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    Quote Originally Posted by -EquinoX-
    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:
    Code:
    int x = 0;
    That is what I mean by using an integer value to initialise x. Or more generically:
    Code:
    T 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.
    Last edited by laserlight; 11-17-2009 at 12:23 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #29
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    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?

  15. #30
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    Quote Originally Posted by -EquinoX-
    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?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 2 of 5 FirstFirst 12345 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating an object of a class inside another class - possible?
    By Programmer_P in forum C++ Programming
    Replies: 34
    Last Post: 08-16-2009, 12:21 AM
  2. Replies: 2
    Last Post: 11-28-2008, 05:15 PM
  3. Unresolved external on vector inside class constructor
    By Mario F. in forum C++ Programming
    Replies: 13
    Last Post: 06-20-2006, 01:44 PM
  4. Declaring an instance of a class inside a class
    By nickodonnell in forum C++ Programming
    Replies: 4
    Last Post: 10-02-2005, 12:46 AM
  5. how do i define a const inside a class under private
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 11-01-2001, 06:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21