【发布时间】:2014-02-08 07:56:24
【问题描述】:
我有一个类的这段代码(这是一个 sn-p):
template<typename T>
class Pos2 {
public:
T x, y;
Pos2() : x(0), y(0) {};
Pos2(T xy) : x(xy), y(xy) {};
Pos2(T x, T y) : x(x), y(y) {};
};
现在,我还有 2 个 typedef:
typedef Pos2<pos_scalar> Pos;
typedef Pos2<size_scalar> Size;
一切都按预期工作,但是当我这样做时:
Pos p(5.5, 6.5);
Size s(3, 8);
p = s;
我收到此错误:
error: conversion from ‘Size {aka Pos2<short int>}’ to non-scalar type ‘Pos’ requested
这是有道理的,但我想知道如何解决它=P
【问题讨论】:
标签: c++ class templates initialization type-conversion