Template & Arays

This is a discussion on Template & Arays within the C++ Programming forums, part of the General Programming Boards category; Hello i'm trying to solve an exersice i've been given , would appreciate help i have to creat a class ...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    12

    Template & Arays

    Hello
    i'm trying to solve an exersice i've been given , would appreciate help

    i have to creat a class with two templates
    and in the main make an array of that class but it keeps poping the error 2955
    the exersice says i can't overload :perator[]
    any ideas ?
    here's what i've for so far

    //Pair.h
    template<class T,class S>
    class Pair
    {

    protected:

    T rison;
    S sheni;

    public:
    Pair(T mfirst,S msecond);
    virtual ~Pair();
    T first(){return rison;};
    S second(){return sheni;};



    };


    //Pair.cpp

    template<class T,class S>
    Pair<T,S>::Pair(T mfirst,S msecond)
    {
    this->rison=mfirst;
    this->sheni=msecond;

    cout <<"Pair has been created "<<endl;

    }


    template<class T,class S>
    Pair<T,S>::~Pair()
    {
    cout <<"Pair has been deleted "<<endl;

    }


    //main

    #include "Pair.h"
    #include "Find.h"
    const int SIZE=5;

    void main()
    {


    double n1;
    char* n2;
    Pair* arr;
    arr = new Pair [SIZE];

    for (int i=0;i<SIZE<i++)
    {
    cout <<"Enter two values "<<endl;
    cout <<"----->";
    cin >>n1;
    n2 = new char[SIZE];
    cin >>n2;
    arr[i]->Pair(n1,n2);
    }


    delete [] arr;
    delete [] n2;


    }


    Now I know I can do this by declaring
    in the class Pair
    T* rishon
    S* sheni
    and overloading the operator[]
    but i need to decalre the Pair* arr in the main and somehow
    create it
    Anyone ?


    thanks in advance

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    You have plenty of problems with your code. Any templated class must be given the template arguments when you use it, so any use of Pair in your code must be changed to Pair<double, char *>. Before you can create an array of class objects, there must be a default constructor. And lastly, main returns int, not void.

    Also, passing char * as a template argument isn't the wisest thing to do because you'll be working with a reference to the memory, not a copy. Any internal state of an object should usually have its own copies of the data. In this case the C++ string class would be much safer.
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    template<class T,class S>
    class Pair 
    {
    protected:
      T rison;
      S sheni;
    public:
      Pair(): rison(T()), sheni(S()){}
      Pair(T mfirst,S msecond);
      virtual ~Pair();
      T first(){return rison;};
      S second(){return sheni;};
    };
    
    template<class T,class S>
    Pair<T,S>::Pair(T mfirst,S msecond)
    {
      this->rison=mfirst;
      this->sheni=msecond;
    
      cout <<"Pair has been created "<<endl;
    }
    
    template<class T,class S>
    Pair<T,S>::~Pair()
    {
      cout <<"Pair has been deleted "<<endl;
    }
    
    const int SIZE=5;
    
    int main()
    {
      double n1;
      string n2;
      Pair<double, string>* arr;
      arr = new Pair<double, string>[SIZE];
    
      for (int i=0;i<SIZE;i++)
      {
        cout <<"Enter two values "<<endl;
        cout <<"----->";
        cin >>n1;
        cin >>n2;
        arr[i] = Pair<double, string>(n1,n2);
      }
      delete [] arr;
    }
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    12

    ok

    thanks for the reply
    one more question do i have to use
    Pair<double, string>* arr
    that kinda spoils the idea of templaete if i delcare <double,string>
    the idea is that each pair will be decided according to input
    sometime double,int sometimes string,string whatever the user chooses ...
    can it be done ?

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >that kinda spoils the idea of templaete if i delcare <double,string>
    How so? You can still use the template class for any combination of valid types. That sounds like an accurate portrayal of parameterized types to me.

    >the idea is that each pair will be decided according to input
    It doesn't quite work like that. You can specify default template arguments that can be used like this:
    Code:
    template<class T = double,class S = string>
    class Pair {
      ...
    }
    
    const int SIZE = 5;
    
    int
    main()
    {
      Pair<> *arr = new Pair<>[SIZE];
    }
    But if the client wants anything other than a (double, string) pair, they have to say so explicitly.

    >can it be done ?
    Only with function templates:
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    template <typename T>
    void foo(T& arg);
    
    int
    main()
    {
      int    i = 10;
      double d = 123.456;
      char   c[] = "testing";
      string s("one...two...three");
    
      foo(i);
      foo(d);
      foo(c);
      foo(s);
    }
    
    template <typename T>
    void
    foo(
      T& arg
      )
    {
      cout<< arg <<endl;
    }
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    May 2004
    Posts
    12

    ok ....

    ok , understood

    and thanks for the fast and accurate replies
    godblles you

    (and Tayshaun Prince)

  6. #6
    Registered User
    Join Date
    May 2004
    Posts
    12
    Ok this is what i got so far I can't figure out what's wrong
    i've searched everywhere

    Code:
    //MAIN
    
    
    #include "Pair.h"
    #include "Find.h"
    const int SIZE=5;
    
    
    template <typename M>
    M send(M& arg);
    void main()
    {
    
    
    	double n1;
    	int   n2;
    	Pair<> *arr = new Pair<>[SIZE];
    
    	for (int i=0;i < SIZE; i++)
    	{
    		cout <<"Enter two values  "<<endl;
    		cout <<"----->";
    		cin >>n1;
    		cin >>n2;
    		arr[i] = Pair<>(send(n1),send(n2));
    	}
    
    		
    	delete [] arr;
    	
    
    
    }
    
    template <typename M>
    M send(M& arg)
    {
      return arg;
    }
    
    
    
    
    //PAIR.H
    
    template<class T = double,class S = int>
    class Pair  
    {
    
    protected:
    	
    	T rison;
    	S sheni;
    	
    public:
    	
    	Pair(): rison(T()), sheni(S()){};
    	Pair(T mfirst,S msecond);
    	virtual ~Pair();
    	T first(){return rison;};
    	S second(){return sheni;};
    
    
    
    };
    
    //PAIR.CPP
    
    template<class T,class S>
    Pair<T,S>::Pair(T mfirst,S msecond)
    {
    	this->rison=mfirst;
    	this->sheni=msecond;
    	
    	cout <<"Pair has been created "<<endl;
    }
    
    
    template<class T,class S>
    Pair<T,S>::~Pair()
    {
    	cout <<"Pair has been deleted "<<endl;
    }
    And this is the error message

    main.obj : error LNK2001: unresolved external symbol "public: __thiscall Pair<double,int>::Pair<double,int>(double,int)" (??0?$Pair@NH@@QAE@NH@Z)
    main.obj : error LNK2001: unresolved external symbol "public: virtual __thiscall Pair<double,int>::~Pair<double,int>(void)" (??1?$Pair@NH@@UAE@XZ)
    Debug/ex2tr2.exe : fatal error LNK1120: 2 unresolved externals
    Error executing link.exe.

    ex2tr2.exe - 3 error(s), 0 warning(s)

    Anyone ?

  7. #7
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Everything that is in Pair.cpp should be put into Pair.h instead. All or nearly all compilers don't support template class method definitions in the source file, they must be in the header file that is included elsewhere.

  8. #8
    Registered User
    Join Date
    May 2004
    Posts
    12

    Ok

    10x that helped , would it work properly on a borland compiler ?

    Now i have another problem ...
    it won't override the default values of <class t = double,class S=int>
    which sort of makes the template abit unusefull

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >it won't override the default values of <class t = double,class S=int>
    Are you specifying different template parameters?
    Code:
    #include <string>
    
    using namespace std;
    
    template<class T = double,class S = string>
    class Pair {
    };
    
    const int SIZE = 5;
    
    int
    main()
    {
      // Use the default
      Pair<> *arr = new Pair<>[SIZE];
      // Change the default
      Pair<int, int> *arr2 = new Pair<int, int>[SIZE];
    }
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Specialising a member function with a template template parameter
    By the4thamigo_uk in forum C++ Programming
    Replies: 10
    Last Post: 10-12-2007, 04:37 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 11:54 AM

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