简体   繁体   English

为什么包含此标头会导致此错误?

[英]why is including this header causing this error?

This is a header and source for a class I have. 这是我的课程的标题和源。 There are similar other classes, all of which have to be included in one central class. 还有其他类似的类别,所有这些都必须包含在一个中央类别中。

However, including the file GameObject.h in the header below causes an error: 但是,在下面的标头中包含文件GameObject.h会导致错误:

error C2582: 'operator =' function is unavailable in 'Node'

Although this error seems a little obscure, I have a slightly older working version of the code, and the only difference is that Node now inherits from GameObject. 尽管此错误似乎有点晦涩难懂,但我的代码的工作版本略旧,唯一的区别是Node现在继承自GameObject。

Header: 标题:

#ifndef NODE_H
#define NODE_H

#include <d3d10.h>
#include <d3dx10.h>
#include "GameObject.h"

class Mesh;

class Node : public GameObject
{
public: 
Node(Mesh& mesh);
};

#endif

source: 资源:

#include "Node.h"
#include "Mesh.h"

Node::Node(Mesh& mesh) : GameObject(mesh)
{

}

In the call stack I am eventually pointed to a declaration of a vector of node objects as the cause, but I have no idea why this is happening as there are other objects that also inherit from gameObject that are also in vectors. 在调用堆栈中,我最终指出节点对象的向量的声明是原因,但我不知道为什么会发生这种情况,因为还有其他对象也从向量中的gameObject继承。

std::vector<Node> nodes;

However, including the file GameObject.h in the header below causes an error: 但是,在下面的标头中包含文件GameObject.h会导致错误:

This implies that the only difference between the working and non-working code is the inclusion of that header file, but , 这意味着有效代码和无效代码之间的唯一区别是该头文件的包含, 但是

I have a slightly older working version of the code, and the only difference is that Node now inherits from GameObject. 我有一个较旧的代码工作版本,唯一的区别是Node现在是从GameObject继承的。

This implies that there is another difference: Node now inherits from GameObject . 这意味着存在另一个区别: Node现在从GameObject继承。 Which is the the inclusion of the inheritance that is causing the problem? 包含哪些导致问题的继承?

Assuming that it is the inheritance that is causing the problem, I guess you have a private or otherwise unavailable operator= in GameObject . 假设是导致问题的继承,我想您在GameObject有一个private或其他不可用的operator= Since vector s like to copy objects around, you'll be needing an operator= available in both GameObject and Node . 由于vector喜欢在周围复制对象,因此您需要在GameObjectNode中都可以使用operator=

In the alternative, you may have a reference inside GameObject that is preventing the compiler from providing its automatic assignment operator, thus preventing Node from having an automatic assignment operator. 或者,您可能在GameObject中有一个引用,该引用阻止编译器提供其自动分配运算符,从而阻止Node具有自动分配运算符。

In sum: If, inside GameObject , you have either a private operator= or a non-static reference (like Mesh& ), then you will need to create an Node::operator= for vector to use. 总而言之:如果在GameObject ,您有一个私有operator=或一个非静态引用(例如Mesh& ),那么您将需要创建一个Node::operator=以便vector使用。

References : 参考文献

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM