本文介绍了模板编译错误:“ X”未引用值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到以下错误:'ComponentManager'在编译从该父类继承的子类时没有引用值

template<typename ComponentManager>
class component_collection {
protected:
  int n_components;
  int n_versions;
  int first_blank;
  int last_used;
  std::vector<std::deque<shared_ptr<entity<ComponentManager>>>> entity_pointers;

public:
  component_collection(int n_c, int n_v) :
    n_components(n_c),
    n_versions(n_v),
    first_blank(0),
    last_used(0),
    entity_pointers(n_v, std::deque<shared_ptr<entity<ComponentManager>>>()) // <-- ERROR HERE
  {}

  ...

};

对于如何初始化 std :: vector 在构造函数中为 n_v 为空的 std :: deque

I am confused as to how I should initialise the std::vector with n_v empty std::deques in the constructor.

子类声明与 deque s类似的 vector 并编译/工作符合预期(直到我将 vector< deque> 添加到父对象,即):

The sub-class declares a similar vector of deques and compiles / worked as expected (until I added the vector<deque> to the parent, that is):

template<typename ComponentManager>
class test_component_collection : public component_collection<ComponentManager> {
  std::vector<std::deque<int>> values;

public:
  test_component_collection(int n_c, int n_v) :
      component_collection<ComponentManager>(n_c, n_v),
      values(n_v, std::deque<int>()) // <-- NO ERROR HERE
  { }

  ...

};

因此,这似乎是与 std的容器有关的:: $ shared_ptr< entity< ComponentManager>>> ,但我不明白为什么会有问题(包含 entity 标头,像您期望的那样使用< ComponentManager> ,并在使用所有这些类时提供 ComponentManager )。

So it seems this is something to do with this being a container of std::shared_ptr<entity<ComponentManager>>, but I don't see why that would be a problem (the entity header is included, takes a <ComponentManager> as you would expect, and a ComponentManager is provided when all these classes are used).

我显然丢失了某些东西...

I am clearly missing something...

更新

以下是实体的代码:

template<typename ComponentManager>
class entity {
protected:
  ComponentManager & component_manager;

public:
  entity(ComponentManager & cm) : component_manager(cm) {}
  void initialise_components(shared_ptr<entity<ComponentManager>> sp) {}
};

component_collection 的顶部添加它解决问题(至少是编译错误,我没有彻底测试结果):

Adding this at the top of component_collection seems to fix things (the compilation error at least, I've not thoroughly tested the results):

template<typename ComponentManager> using entity_ptr_deque = std::deque<shared_ptr<entity<ComponentManager>>>;

然后将 entity_ptr_deque< ComponentManager 替换为适当。这有助于提高可读性,因此无论如何我都可以保留它,但是它无助于理解错误,特别是因为一个最小的示例构建得很好(请参见评论)。

And then substituting entity_ptr_deque<ComponentManager in as appropriate. This aids readability, so I might leave it in anyway, but it doesn't help understand the error, especially as a minimal example builds fine (see comments).

更新2

使用从评论中,我发现我的完整课程没有编译,而@VittorioRomeo编辑的版本却可以编译。

Using Wandbox from the comments, I found that my full class didn't compile, while the version edited by @VittorioRomeo did.

在Wandbox上从Clang切换到GCC(我在本地使用Clang)给出了非常不同的错误消息:

Switching from Clang to GCC on Wandbox (I'm using Clang locally) gave a very different error message:

declaration of 'auto component_collection<ComponentManager>::entity(int)'
changes meaning of 'entity'

添加 std :: vector< std :: deque< shared_ptr< entity< ComponentManager>>> entity_pointers; 和相关的构造函数更新。当然,它会在类定义中屏蔽名称 entity ,因此会出现错误。 Clang的错误消息完全指向其他地方。

This is a method that was present before I added the std::vector<std::deque<shared_ptr<entity<ComponentManager>>>> entity_pointers; and associated constructor update. It of course masks the name entity in the class definition, hence the error. Clang's error message pointed somewhere else entirely.

推荐答案

Clang编译错误'X'不涉及值可能会误导人。

The Clang compilation error 'X' does not refer to a value can be misleading.

这意味着,在代码中,Clang期望的是 ,不是类型。但这可能与 X 的性质无关。

It means, at that point in the code, Clang is expecting a value, not a type. But the reason for that may be nothing to do with the nature of X.

code> X 被传递给 期望值而不是类型,即Clang不认为这是模板。

It may be that whatever X is being passed to expects a value rather than a type, i.e. Clang does not think this is a template.

具体在这种情况下: template< typename ComponentManager>实体已由类中的方法掩盖– auto实体(int)。这会更改 entity 的含义,从而在模板专门化的位置导致错误,但在正在执行方法的方法的位置发生 not 错误

Specifically in this case: template<typename ComponentManager> entity has been masked by a method in the class – auto entity(int). This changes the meaning of entity, causing an error at the site of the template specialisation, but not at the site of the method which is doing the masking.

在这种情况下,GCC给出了更清晰的错误消息,因此值得尝试使用看看不同的编译器认为代码有什么问题。

GCC gives a much clearer error message in this instance, so it's worth trying a tool like Wandbox to see what different compilers think is wrong with the code.

这篇关于模板编译错误:“ X”未引用值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 11:54