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; what is wrong with the implementation of the add function for the vector class here? Code: namespace project { template ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    569

    templatized class inside templatized class

    what is wrong with the implementation of the add function for the vector class here?

    Code:
    namespace project {
    template <class _T>
    class type
    {
     public :
     	type();
     	type(int num);
    
     	type( const type & typ);
     	~type();
    
     protected :
     	_T val;
    
    };
    
    template <class type, size_t n>
    class vector
    {
      public:
    
      // constructors/destructor
        vector( );                        // default constructor (size==0)
       
        vector( const vector & vec );     // copy constructor
        ~vector( );                       // destructor
    
      // operator overloads
        const vector & operator = ( const vector<type, n>& vec );
    
      // add element to a vector
        void add(type value);
     
        vector<type, n>& operator+=(const vector<type, n>& vec);
        vector<type, n>& operator-=(const vector<type, n>& vec);
    
      // unary
        vector<type, n>& operator-();	
    	
      // member functions
        int  length( ) const;                  // capacity of vector
    
      //read access operator for a polynomial
        const type& operator[]( int index ) const;
    	
      //write access operator for a polynomial
        type& operator[]( int index );
    
     protected:    
        int csize;
        int  size;                             // # elements in array
        type * list;                          // array used for storage
        
    };
    
    template <class type, size_t n>
    void vector<type, n>::add(type value)
    {
     this.list[this.csize] = value;
     this.csize++;
    }
    }
    Last edited by -EquinoX-; 11-16-2009 at 08:22 PM.

  2. #2
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    you have to put the method implementation inside the class declaration.

    Code:
    template <class _T>
    class type
    {
     public :
     	type();
     	type(int num);
    
     	type( const type & typ);
     	~type();
    
     protected :
     	_T val;
    
    };
    
    template <class type, size_t n>
    class vector
    {
      public:
    
      // constructors/destructor
        vector( );                        // default constructor (size==0)
       
        vector( const vector & vec );     // copy constructor
        ~vector( );                       // destructor
    
      // operator overloads
        const vector & operator = ( const vector<type, n>& vec );
    
      // add element to a vector
        void vector::add(type value)
        {
         this.list[this.csize] = value;
         this.csize++;
        }
    
     
        vector<type, n>& operator+=(const vector<type, n>& vec);
        vector<type, n>& operator-=(const vector<type, n>& vec);
    
      // unary
        vector<type, n>& operator-();	
    	
      // member functions
        int  length( ) const;                  // capacity of vector
    
      //read access operator for a polynomial
        const type& operator[]( int index ) const;
    	
      //write access operator for a polynomial
        type& operator[]( int index );
    
     protected:    
        int csize;
        int  size;                             // # elements in array
        type * list;                          // array used for storage
        
    };
    also, what is the purpose of n and the class "type"? you are probably going down the wrong road with those.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    n is the vector size which is implemented as an array

    the requirements is:

    •Your template class should be called vector. Note that the template parameters for your vector
    class will necessarily be different from those of the std::vector class.
    • Your vectors should have elements of type, with fixed length n.
    • Elements of type must support the full set of operators provided below‡. Elements must be able
    to take as their optional constructor argument an int.

    I don't really think that the implementation must be inside the class

    I got the following error when trying to use the add:
    vector_main.cpp:13: error: no matching function for call to ‚ece373::vector<int,u>::add(ece373::type<int>&)‚
    vector.h:171: note: candidates are: void ece373::vector<type, n>::add(type) [with type = int, unsigned int n = 3u]
    Last edited by -EquinoX-; 11-16-2009 at 07:57 PM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,616
    Quote Originally Posted by -EquinoX-
    what is wrong with the implementation of the add function for the vector class here?
    You forgot that this is a pointer.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    so how can I fix this?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,616
    You can either remove the this. or change them to this->.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    Code:
    I don't really think that the implementation must be inside the class
    d'oh of course. that is correct. you just can't put it in a separate file.

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    Quote Originally Posted by laserlight View Post
    You can either remove the this. or change them to this->.
    I even removed that and it still gives me the error... I think the problem is in the function signature

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,616
    Quote Originally Posted by -EquinoX-
    I even removed that and it still gives me the error... I think the problem is in the function signature
    Then post the smallest and simplest program that demonstrates the error.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    here's my test:

    Code:
    using namespace project;
    
    int main(){
     type<int> e(1);
     vector<int, 3> a;
     a.add(e);
    }
    Last edited by -EquinoX-; 11-16-2009 at 08:22 PM.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,616
    Notice that vector is a vector<int, 3>, not a vector<type<int>, 3>.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    ok now I got a different error:

    vector_main.cpp.text+0xb0): undefined reference to `project::type<int>::type(project::type<int> const&)'
    CMakeFiles/vector.dir/vector_main.cpp.o: In function `project::vector<project::type<int>, 3u>::vector()':
    vector_main.cpp.text._ZN6project6vectorINS_4typeIiEELj3EEC1Ev[project::vector<project::type<int>, 3u>::vector()]+0x5a): undefined reference to `project::type<int>::type()'


    FYI: I encapsulate the whole .h file above into a namespace called project, see my edited code above

  13. #13
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    reading your assignment, it is my interpretation that your professor did not intend for you to implement a separate class called type.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,616
    You declared but did not define the copy constructor for project::type. You did not define the default constructor for project::type, but it is used by project::vector's default constructor.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    I've attached my code for you to see, I have the constructor and everything. I have the constructor and everything, and 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

Page 1 of 5 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-15-2009, 11:21 PM
  2. Replies: 2
    Last Post: 11-28-2008, 04:15 PM
  3. Unresolved external on vector inside class constructor
    By Mario F. in forum C++ Programming
    Replies: 13
    Last Post: 06-20-2006, 12:44 PM
  4. Declaring an instance of a class inside a class
    By nickodonnell in forum C++ Programming
    Replies: 4
    Last Post: 10-01-2005, 11:46 PM
  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, 05:01 PM

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