Thread: long question; short answer <templates>

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    719

    long question; short answer <templates>

    i have 2 class that are templates. i want a vector of one class to
    be a composite member of another class. the will be vector<firstclass>

    let me explain in code...without templates the classes would look like this...

    ...by the way....i'm having trouble with the declaration part;

    Code:
    class A
    {
         int notimportant;
    };
    
    class B
    {
         vector<A> somevectoroftypeA;
    };
    now with templates....

    Code:
    template <class T> class A
    {
         T notimportant;
    };
    
    template <class T> class B
    {
         //cannot figure out how to declare this
         vector<A<T>> variablename;   //?????????????
    };

    please help


    [/code]

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Try an extra space between the last two '>'

    vector<A<T> > variablename;

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    the compiler tells me i need a template argument list or something like that

    i don't even know if i'm anywhere close with this:
    vector<A<T> > variablename;

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Quote Originally Posted by misplaced
    the compiler tells me i need a template argument list or something like that

    i don't even know if i'm anywhere close with this:
    vector<A<T> > variablename;

    Code:
    #include <vector>
    using std::vector;
    
    template <class T> class A
    {
         T notimportant;
    };
    
    template <class T> class B
    {
      
         vector<A<T> > variablename;   
    };
    
    int main() 
    {
    	B<int> b;
    }

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    19
    I hope this example helps you:

    CFirst.h
    Code:
    template<typename T>
    class CFirst  
    {
    public:
    	CFirst() {};
    	virtual ~CFirst() {};
    
    private:
    	T m_member;
    };
    CSecond.h
    Code:
    #include "First.h"
    #include <vector>
    
    template <typename T>
    class CSecond  
    {
    public:
    	CSecond(const T& x) : m_member(x) {};
    	virtual ~CSecond() {};
    
    private:
    	T m_member;
    	CFirst<T> m_member2;
    	std::vector<CFirst<T> > m_member3;
    };
    Dummy.cpp
    Code:
    #include "First.h"
    #include "Second.h"
    
    int main(int argc, char* argv[])
    {
    	CFirst<int> a;
    	CSecond<int> b(4);
    
    	return 0;
    }
    mfg JJ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help calling function is asm
    By brietje698 in forum C++ Programming
    Replies: 24
    Last Post: 12-06-2007, 04:48 PM
  2. 'short' question
    By sand_man in forum C Programming
    Replies: 5
    Last Post: 03-20-2005, 05:04 PM
  3. long long question
    By XSquared in forum C Programming
    Replies: 3
    Last Post: 04-12-2004, 10:16 AM
  4. Replies: 1
    Last Post: 01-23-2002, 03:34 AM
  5. How to display 23 bytes long double answer in C?
    By CLIE_ZETA in forum C Programming
    Replies: 3
    Last Post: 11-18-2001, 12:11 PM