相互包含头文件会导致问题

时间:2017-05-07 14:53:05

标签: c++ header include

我有两个相互包含的头文件。 由于这创建了错误,我做了这个网站建议并删除了包含,使其中一个实例成为指针,并使用了前向声明。

然而,发生了其他错误,我不知道其来源或修复。 非常感谢提前。

Code dump inc。

---------------- team.h ------------------

#ifndef TEAM_H
#define TEAM_H

#include <exception>
#include "avl.h"
#include "pair.h"

class Team {
private:

    class Mutant; //Forward Declaration

    int team_ID;
    Avl<int, Mutant>* team_mutants_by_ID;
    Avl<Pair<int, int>, Mutant>* team_mutants_by_power;
    int team_most_powerful;

public:
    // C'tor
    Team(int new_team_ID);

    // D'tor
    ~Team();

    // Returns the team's mutant tree that is ID sorted.
    Avl<int, Mutant>* GetTeamMutantsByID();

    // Returns the team's mutant tree that is sorted by power.
    Avl<Pair<int, int>, Mutant>* GetTeamMutantsByPower();

}; // Team

#endif

---------- team.cpp -------------

#include "team.h"

const int EMPTY = -1;

// C'tor
Team::Team(int new_team_ID)
 {
    team_ID = new_team_ID;
    team_mutants_by_ID = new Avl<int, Mutant>();
    team_mutants_by_power = new Avl<Pair<int, int>, Mutant>();
    if (!team_mutants_by_ID || !team_mutants_by_power) {
        delete team_mutants_by_ID;
        delete team_mutants_by_power;
        throw AVL_ALLOCATION_ERROR();
    }
    team_most_powerful = EMPTY;
}

// D'tor
Team::~Team() {
    delete team_mutants_by_ID;
    delete team_mutants_by_power;
}

// Returns the team's mutant tree that is ID sorted.
Avl<int, Mutant>* Team::GetTeamMutantsByID() {
    return team_mutants_by_ID;
}

// Returns the team's mutant tree that is sorted by power.
Avl<Pair<int, int>, Mutant>* Team::GetTeamMutantsByPower() {
    return team_mutants_by_power;
}

---------- -------------- mutant.h

#ifndef MUTANT_H
#define MUTANT_H

#include <iostream>

class Mutant {
private:
    class Team; // forward declaration

    int student_ID;
    int grade;
    int power;
    Team* team;

public:
    // C'tor
    Mutant(int student_ID, int grade, int power);

    // Default C'tor (for ghost)
    Mutant();

    // D'tor
    ~Mutant();

    // Returns a pointer to the team the mutant belongs to
    // If the mutants does not belong to a team - returns NULL
    Team* GetTeam();

}; // Mutant

#endif

---------- -------------- mutant.cpp

#include "mutant.h"

const int EMPTY = -1;
// C'tor
Mutant::Mutant(int student_ID, int grade, int power) : 
student_ID(student_ID), grade(grade), power(power), team(NULL) {
 }

// Default C'tor (for ghost)
Mutant::Mutant() : student_ID(EMPTY), grade(EMPTY), power(EMPTY), team(NULL)     
{
};

// D'tor - Default is enough
Mutant::~Mutant() {
}
}

// Returns a pointer to the team the mutant belongs to
// If the mutants does not belong to a team - returns NULL
Team* Mutant::GetTeam() {
    return team;
}

错误是: 标识符&#34;团队&#34;未定义(mutant.cpp)

声明与&#34; Mutant :: Team * Mutant :: GetTeam()&#34;不兼容(mutant.cpp)

int * Mutant :: GetTeam(void):重载函数的区别仅在于Mutant :: Team * Mutant :: GetTeam(void)(mutant.cpp)的返回类型

team.cpp中有类似的错误

如果需要,我可以提供其他信息。 谢谢你的进步。

1 个答案:

答案 0 :(得分:0)

当编译器尝试编译Team* Mutant::GetTeam()时,它会将Team视为属于全局范围。但是,Team不属于全局范围,因为在编译Mutant.cpp时编译器可以判断的唯一位置是Mutant,前向参考。所以,Mutant::Team* Mutant::GetTeam()可能会有用,虽然它可能会在以后引起问题,因为Team实际上并不是Mutant中的嵌套类。将前向引用放在类Team外的<{1}} 之外可能会有效。

但是,最好Mutant内的#include "team.h",并删除mutant.hTeam的前瞻声明。

如果这会给你带来错误,那么这将是另一个stackoverflow问题的主题。