所有编辑都很好。但输出包含一个我不明白的奇怪错误。如果有人可以帮助我,那会很棒。
主要():
vector<PointGeneric<float>> v(3);
for (int i = 0; i < v.size(); i++)
{
v[i] = {(float)(i*1.1), (float)(i*0.9)};
cout << "1: v[" << i <<"] = " << v[i] << endl;
}
for (int i = 0; i < v.size(); i++)
cout << "2: v[" << i <<"] = " << v[i] << endl;
输出:
1: v[0] = (0.000000, 0.000000)
1: v[1] = (1.100000, 0.900000)
1: v[2] = (2.200000, 1.800000)
2: v[2] = (2.200000, 1.800000)
2: v[2] = (2.200000, 1.800000)
2: v[2] = (2.200000, 1.800000)
模板类:(不是我自己的,取自github上的“svg-curve-lib”库并且稍加修改;在原始上下文中一切正常)
template <typename Tx = float, typename Ty = Tx>
struct PointGeneric
{
public:
struct x_getter;
struct y_getter;
x_getter x{this};
y_getter y{this};
PointGeneric() : PointGeneric { 0, 0} {}
PointGeneric(Tx x, Ty y) : x(this), y(this), _x{x}, _y{y} {};
...
private:
Tx _x;
Ty _y;
};
x_getter - struct:
template <typename Tx = float, typename Ty = Tx>
struct PointGeneric<Tx, Ty>::x_getter
{
public:
x_getter(PointGeneric *t): thisPointer{t} {};
operator Tx() const {return this->thisPointer->_x;};
private:
PointGeneric<Tx, Ty>* thisPointer;
};
答案 0 :(得分:0)
至少在显示的代码中,PointGeneric
没有用户定义的复制构造函数或赋值运算符,x_getter
也没有。在
PointGeneric<float> source;
PointGeneric<float> dest = source;
您将dest.x.thisPointer
指向source
,而不指向dest
(同样适用于dest.y
;并且必须经过必要的变更指定。)
在您的示例中,您有
v[i] = {(float)(i*1.1), (float)(i*0.9)};
在这里,source
是一个临时的,在分号处被破坏,留下v[i].x.thisPointer
一个悬空指针。我假设(虽然没有显示)表达式cout << v[i]
最终调用v[i].x.operator float()
,而thisPointer
又会尝试尊重'\"C:\Program Files\Sublime Text 3\sublime_text.exe\"' is not recognized as an internal or external command,
operable program or batch file.
,因此您的程序会显示未定义的行为。