【发布时间】:2016-11-18 11:44:07
【问题描述】:
代码如下:
#include <iostream>
using namespace std;
template<class OwnerType>
class Move {
public:
Move() {}
Move(OwnerType &_owner) {
owner = &_owner;
}
void GetPosition() {
cout << owner->x << endl;
}
OwnerType *owner;
};
class Entity {
public:
int x = 50;
Move<Entity> *move;
};
int main() {
Entity en;
en.x = 77;
en.move = new Move<Entity>(en); // sign '=' is underlined by VS
en.move->GetPosition();
return 0;
}
它给出的错误:
a value of type "Move<Entity> *" cannot be assigned to an entity of type "Move<Entity> *"
程序正在编译,按预期工作并给出预期值,但错误仍然存在。 这可能与模板和编译时间和其他东西有关,但我没有足够的知识来知道这个错误实际上代表什么。
也不要担心泄漏,因为这只是我测试,错误是我不明白的。
提前致谢。
【问题讨论】:
-
不要相信智能感知。实际编译。
-
[OT]:你的程序泄露了。
-
int main {在您的实际代码中吗?错过了()。 -
这只是为了测试是否可以做一些事情,所以我不太关心删除指针,我也忘了 () 复制粘贴时不知何故我会纠正它,但是问题本身就是错误。
标签: c++ templates compilation