Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 作为类参数的模板对象在编译前出错_C++_Templates_Compilation - Fatal编程技术网

C++ 作为类参数的模板对象在编译前出错

C++ 作为类参数的模板对象在编译前出错,c++,templates,compilation,C++,Templates,Compilation,代码如下: #include <iostream> using namespace std; template<class OwnerType> class Move { public: Move() {} Move(OwnerType &_owner) { owner = &_owner; } void GetPosition() { cout << owner->x &

代码如下:

#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;
}
#包括
使用名称空间std;
模板
阶级运动{
公众:
移动(){}
移动(所有者类型和所有者){
所有者=&_所有者;
}
void GetPosition(){
cout x GetPosition();
返回0;
}
它给出的错误:

a value of type "Move<Entity> *" cannot be assigned to an entity of type "Move<Entity> *"
不能将“Move*”类型的值分配给“Move*”类型的实体
程序正在编译,按预期工作并给出预期值,但错误仍然存在。 这可能与模板、编译时间等有关,但我没有足够的知识知道这个错误实际上代表了什么

也不要担心泄漏,因为这只是我的测试,错误是我不理解的

提前感谢。

所以这不是错误

这是智能感知: 请参阅:


旧的:

您的
main
需要
()

这对我很有用:

#include<iostream>
using namespace std;

template<class T> class Move {
public:
    Move() {}
    Move(T &_owner) {
        owner = &_owner;
    }
    void GetPosition() {
        cout << owner->x << endl;
    }
    T *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;
}

众所周知,Intellisense显示无效错误(请参见示例),请信任编译器和链接器,如注释中所示

但是,此错误非常烦人,请尝试关闭解决方案,删除
.suo
文件(它是隐藏的),然后再次打开。此处提供了有关
.suo
文件的详细信息


旁注,在您的代码示例中,
main
缺少
()

不要信任intellisense。实际编译。[OT]:您的程序泄漏。
int main{
是您的实际代码吗?缺少
()
。这只是为了测试是否可以执行某些操作,所以我不太关心删除指针,而且我忘记了()当复制粘贴时,我会以某种方式更正它,但问题是错误本身。OP的可能副本表示它有效,是无效错误使他感到不适。它工作正常,但在编译之前就出现错误,而且我忘了说使用了Visual Studio 2015。Intellisense是的。谢谢帮助。是的,这几乎解释了一切。谢谢。
77