help with templates

This is a discussion on help with templates within the C++ Programming forums, part of the General Programming Boards category; hi all how are you doing. I have a quick question involving templates, I unfortunatley have a horrible professor for ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    78

    help with templates

    hi all how are you doing. I have a quick question involving templates, I unfortunatley have a horrible professor for my data structures programming class at the university I'm attending. So I thought I might get ahold of you fellas here to see if you could help me out
    I just need to see a very rudementry program that uses templates for three different data types int char and strings. All of them should be used in one data member in a class declaration. I'm totally lost right at the moment. The guy teaching the class really left us out to dry on this one. so far this is what I have to work with


    Code:
    #include <iostream.h>
    #include <string.h>
    //class4.cpp
    const int n=3;
    #include <iostream.h>
    #include <string.h>
    
    
    template <class T>
    
    class myclass
    {
    	private:
    
    		T a[n];
    	public:
    		void readdata();
    		void displaydata();
    		friend void displaydata(myclass p);
    
    };
    //----------------------------------------------------------------------------
    template <class T>
    void displaydata(myclass<T> q)
    {
    	for(int i=0; i<n; ++i)
    		cout<<q.a[i]<<endl;
    }
    //-----------------------------------------------------------------------------
    
    template <class T>
    void myclass<T> ::readdata()
    {
    	for(int i=0; i<n; ++i)
    	{
    		cout<<"Enter data:"; 
    		 cin>>a[i];
    	}
    }
    //-----------------------------------------------------------------------------
    
    template <class T>
    void myclass<T> :: displaydata()
    {
    	for(int i=0; i<n; ++i)
    		cout<<a[i]<<endl;
    }
    //-----------------------------------------------------------------------------
    
    void main()
    {
    	myclass<float> p;
    	p.readdata();
    	p.displaydata();
    	displaydata(p);
    }
    If anybody could at least point me in the right direction it would be much appreciated

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    A few things:

    Update your headers if you can
    Code:
    //if you have a new enough compiler (standards compliant)
    #include <iostream> //the standard headers do not have 
    #include <string>     // the .h extension
    
    using namespace std;
    I'm not sure why you need a friend function here...
    Code:
    const int n=3;
    
    
    template <class T>
    
    class myclass
    {
    	private:
    
    		T a[n];
    	public:
    		void readdata();
    		void displaydata();
    	                 //friend void displaydata(myclass p);
    
    };
    Then for your input functions you might want to do something like this:
    Code:
    //----------------------------------------------------------------------------
    template <class T>
    void displaydata(myclass<T> q)
    {
    	for(int i=0; i<n; ++i)
    		cout<<q.a[i]<<endl;
    }
    //-----------------------------------------------------------------------------
    
    //template <class T>
    void myclass<float> ::readdata()
    {
    	for(int i=0; i<n; ++i)
    	{
    		cout<<"Enter data (float):"; 
    		 cin>>a[i];
    	}
    }
    void myclass<int> ::readdata()
    {
    	for(int i=0; i<n; ++i)
    	{
    		cout<<"Enter data (int):"; 
    		 cin>>a[i];
    	}
    }
    
    //-----------------------------------------------------------------------------
    
    template <class T>
    void myclass<T> :: displaydata()
    {
    	for(int i=0; i<n; ++i)
    		cout<<a[i]<<endl;
    }
    //-----------------------------------------------------------------------------
    
    int main()
    {
    	myclass<int> p;
    	p.readdata();
    	p.displaydata();
    	
    }
    But its late and I'm tired so I might not understand your problem correctly
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Looks pretty good. Your friend function needs to be declared a templated function (myclass is an incomplete type in itself). If you specialize your functions as JaWiB was showing, you need syntax like:
    Code:
    template<>
    void foo<int>::bar( ) { }
    And, when changing your headers, be careful about the distinction between <string> and <cstring>. The old <string.h> is now <cstring>, and contains functions such as strcpy, strcmp, etc. <string> has the std::string class.

    Cheers.

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    As a side note, it helps if you post your specific problem. E.g. Abnormal run-time behavior (and what it is), or specific compiler errors.

  5. #5
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Change your friend-declaration into this:
    Code:
    template <typename T>
    friend void displaydata(myclass<T> p);
    and it'll work.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Templates from DLL or static library problem
    By mikahell in forum C++ Programming
    Replies: 2
    Last Post: 01-01-2008, 12:49 AM
  2. Questions about Templates
    By Shamino in forum C++ Programming
    Replies: 4
    Last Post: 12-17-2005, 11:22 PM
  3. templates and inheritance problem
    By kuhnmi in forum C++ Programming
    Replies: 4
    Last Post: 06-14-2004, 02:46 AM
  4. When and when not to use templates
    By *ClownPimp* in forum C++ Programming
    Replies: 7
    Last Post: 07-20-2003, 09:36 AM

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