简体   繁体   English

使用类型类的向量时获取“未声明的标识符”

[英]Getting 'undeclared identifier' when using vector of type class

Having trouble understanding why I'm getting an 'undeclared identifier' error when I've made sure to include the header file that has the declaration of the class I'm making a vector of. 当我确保包含具有我要作为其向量的类的声明的头文件时,无法理解为什么会收到“未声明的标识符”错误。

#pragma once
#include <vector>
#include "Member.h"

class Party {
private:
    std::vector<Member> members;

public:
    Party();
    int get_party_size();
    void add_member(Member new_member);
    Member& get_member(int num);

};

Here's "Member.h" 这是“ Member.h”

#pragma once
#include <vector>
#include <string>
#include "Party.h"

class Member
{
private:
    int hp;
    bool is_stunned;
    bool is_alive;

public:
    Member();
    ~Member();
    int get_hp();
    bool get_is_stunned();
    bool get_is_alive();
    void take_damage(int amt);
    void stun();

    virtual void turn(std::vector<Party>& parties, int my_party, int my_member_number);
    virtual std::string get_class_name();

};

Pretty new to the language, so sure I'm missing something obvious. 该语言相当新,因此请确保我缺少明显的内容。

You have circular dependency between Member and Party 您在会员和聚会之间有循环依赖

Remove the line 删除线

virtual void turn(
  std::vector<Party>& parties, 
  int my_party, 
  int my_member_number);

in Member and remove the #include "Party.h" in Member.h 在Member中并删除#include "Party.h"中的#include "Party.h"

Instead think along the lines that a Party is just a collection of Members so there is no need for an individual Member to know about the container 相反,请按照以下思路考虑:一个缔约方只是成员的集合,因此不需要单个成员了解该容器

So after input from @some-programmer-dude you could also solve it by adding a forward declaration in your Member.h instead of including the Party.h 因此,在@ some-programmer-dude输入之后,您还可以通过在Member.h中添加前向声明来解决此问题,而不是包括Party.h

class Party;

class Member { ... }

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

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