Thread: Using a templated object in another class

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    94

    Using a templated object in another class

    This may sound odd, and I know I've been on this board all night bombarding you poor souls with my stupid questions, but I've got another. I have a class that is template based. It looks like this:

    Code:
    template <int A, int B>
    class CDRAWBLT
    {
    ...
    };
    Now, in another class, I have a member function which takes as a parameter a CDRAWBLT reference. My problem is this: I don't know how to get this to work! I've tried everything I can think of to get this to work! I've tried making that one member function template-based, but that doesn't seem to be working, and I've tried to make the whole class template based, but I don't really want that. I've also tried just leaving out the template list while making my other function. I didn't really expect that to work, and it didn't, but anyways...I'm sorry I keep badgering you all, but you're always so helpful! I'm asking one last time (tonight, at least) for your help. Thanks in advance!

    Brendan
    Draco dormiens nunquam titillandus

  2. #2
    Unregistered
    Guest
    Try this:
    Code:
    template <class A, class B>
    class Tester
    {
    	public:
    	A a;
    	B b;
    	void print()
    	{
    		cout<<a<<","<<b<<endl;
    	}
    	void  set(A x, B y)
    	{
    		a = x;
    		b = y;
    	}
    };
    
    class UseTester {
    public:
    	void UseIt(Tester<int,int>& t)
    	{
    		t.print();
    	}
    };
    
    int main()
    {
         Tester<int,int> t;
         UseTester ut;
    
         t.set(4,5);
         ut.UseIt(t);
    
         return 0;
    }
    In your code, you were making the int a specialization of the template class. Ie. template <int A, int B> class X{...};
    Was this what you wanted? Maybe that was what was causing you problems...

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    94
    Oh thank you so so much! That worked!!! Thank you thank you thank you!!!

    Brendan
    Draco dormiens nunquam titillandus

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    94
    And no, int is what I wanted. The point of the template was to size a 2d array. Basically, the CDRAWBLT class holds info on what I call "console bitmaps". Obviously, not every bitmap is the same size, so I couldn't just have something like "array[10][10]". So, templates allowed me to size the array according to each object's needs. See what I'm getting at? Thanks so so much again!

    Brendan
    Draco dormiens nunquam titillandus

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  2. linker error
    By tasha302 in forum C++ Programming
    Replies: 3
    Last Post: 12-14-2006, 12:00 AM
  3. Mmk, I give up, lets try your way. (Resource Management)
    By Shamino in forum Game Programming
    Replies: 31
    Last Post: 01-18-2006, 09:54 AM
  4. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM
  5. C++ Class Object Collection
    By Visual Develope in forum C++ Programming
    Replies: 3
    Last Post: 05-04-2002, 04:48 PM