Need help in class template
Sep 30, 2013 at 10:07am UTC
Hi There
This is my Pair.h file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
#include <iostream>
#include <string>
using namespace std;
#ifndef PAIR_H
#define PAIR_H
template <typename T>
class pair
{
private :
T first;
T second;
public :
Pair(const T& f, const T& s);
T getFirst();
T getSecond();
void setFirst(const T& f);
void setSecond(const T& s);
void print();
};
template <typename T>
Pair<T>::Pair(const T& f, const T& s)
{
first = f;
second = s;
}
template <typename T>
T Pair<T>::getFirst()
{
return first;
}
template <typename T>
T Pair<T>::getSecond()
{
return second;
}
template <typename T>
void Pair<T>::setFirst(const T& f)
{
first = f;
}
template <typenameT>
void Pair<T>::setSecond(const T& s)
{
second = s;
}
template <typenameT>
void Pair<T>::print()
{
cout << first << " " << second << endl;
}
#endif
And my problem is in my .cpp file
I don't know how to create an object stringPair with Pair class template substituted with type string and initialized with values "Quite" and "Dirty"
And how to call the print function to show the objects???
Like this??? correct???
1 2 3 4 5 6 7
#include "Pair.h"
int main()
{
Pair<string> stringPair("Quite" , "Dirty" );
Pair<double > doublePair(1.2, 2.4);
}
Last edited on Sep 30, 2013 at 10:10am UTC
Sep 30, 2013 at 10:14am UTC
yes, you just have to fix the typos (pair, typenameT)
Sep 30, 2013 at 10:37am UTC
have any example????
I still don't understand (pair, typenameT)......
Sep 30, 2013 at 10:45am UTC
its just typo's, the constructor name and the class name capitalization typo: pair instead of Pair, and the missing space typo: typenameT instead of typename T
You should learn the errors:
error: ISO C++ forbids declaration of ‘Pair’ with no type [-fpermissive]
and
error: ‘typenameT’ has not been declared
which both indicate the given typos
Last edited on Sep 30, 2013 at 10:50am UTC
Sep 30, 2013 at 11:36am UTC
I have one more question....
How to call the print function to show the objects???
1 2
cout << stringPair.print();
cout << doublePair.print();
like this???
Topic archived. No new replies allowed.