以下是代碼之前:模板對象作爲類參數給出錯誤編譯
#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 {'是在你的實際代碼中?錯過了'()'。 –