Hello. My problem is quite simple and semantic. So I built a simple class template,
which has one generic type which by default is double.
So I expected to be able to create an object like
point p(1, 2);
,
instead I am forced to write the ugly
point<> p(1, 2);
.
My question is: is there a way to fix that somehow while still keeping the same functionality?
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
|
#include <iostream>
template<class type = double> //default is double
class point
{
private:
type x, y;
public:
point::point(const type &X = 0, const type &Y = 0)
: x(X), y(Y)
{}
type get_x() { return x; }
type get_y() { return y; }
};
int main()
{
point<double> p1(1, 2); //OK
point<> p2(1, 2); //OK
point p3(1, 2); //error
std::cin.get();
}
|
Last edited on
Well, I managed to fix it with a simple typedef
, because unfortunately, that is the C++ syntax.
Last edited on