以下是代码之前:模板对象作为类参数给出错误编译
#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]:您的程序泄漏。 – Jarod42
'int main {'是在你的实际代码中?错过了'()'。 –