0
why wont this work
#include <iostream> using namespace std; template <class T> class Div { public: Div (T x, T y) { cout <<x / y<<endl; } }; template < > class MyClass<char> { public: MyClass (char x) { cout << x <<endl; } }; int main () { string a, b; cin >> a >> b; int x, y; cin >> x >> y; Div <string> d2(a, b); Div <int> d1(x, y); }
2 Respuestas
+ 2
Mando
Because that is not a right template. You have to make template of Div class. My class doesn't not exist.
Check this line:
Div <string> d2(a, b);
And also you have to calculate division of length of string x and y where x is a and y is b
template < >
class Div<string> {
public:
Div (string x, string y) {
cout << x.size() / y.size() <<endl;
}
};
+ 2
#include <iostream>
using namespace std;
template <class T>
class Div {
public:
Div (T x, T y) {
cout<<x / y<<endl;
}
};
template <class T>
class MyClass {
public:
MyClass (char x) {
cout << x <<endl;
}
};
int main () {
char a, b;
cin >> a >> b;
int x, y;
cin >> x >> y;
Div <char> d2(a, b);
Div <int> d1(x, y);
MyClass <char> mC(a);
}