C ++前向声明错误

时间:2010-11-16 17:33:34

标签: c++ forward-declaration

我有一个像这样的错误

    In file included from Level.hpp:12,
                 from main.cpp:4:
Corridor.hpp: In method `void Game::Corridor::update()':
Corridor.hpp:41: invalid use of undefined type `class Game::Level'
Corridor.hpp:13: forward declaration of `class Game::Level'
Corridor.hpp:42: invalid use of undefined type `class Game::Level'
Corridor.hpp:13: forward declaration of `class Game::Level'
Corridor.hpp:43: invalid use of undefined type `class Game::Level'
Corridor.hpp:13: forward declaration of `class Game::Level'
Corridor.hpp:44: invalid use of undefined type `class Game::Level'
Corridor.hpp:13: forward declaration of `class Game::Level'

走廊和水平......

  // Corridor.hpp

#ifndef GAME_CORRIDOR_HPP
#define GAME_CORRIDOR_HPP

#include <Moot/Math.hpp>

//#include <Level.hpp>
#include <GameWindow.hpp>

namespace Game
{
    class Level; // <-- LINE 13

    class Corridor
    {
        static const unsigned int defaultLevelDepth = 800;

        Moot::Math::Vector3D wp1, wp2, wp3, wp4;
        Moot::Math::Vector2D sp1, sp2, sp3, sp4;

        Level * p_level;

    public:

        Corridor(Moot::Math::Vector3D setFirstPoint, Moot::Math::Vector3D setSecondPoint) 
        {
            wp1 = setFirstPoint;
            wp2 = setSecondPoint;

            wp3 = setFirstPoint;
            wp3.z += defaultLevelDepth;

            wp4 = setSecondPoint;
            wp4.z += defaultLevelDepth;
        }


        void update() {

            sp1 = p_level->getLevelCamera().convert3DVectorWithScreenAlgorithm(wp1); // <- LINE 41 etc.
            sp2 = p_level->getLevelCamera().convert3DVectorWithScreenAlgorithm(wp2);
            sp3 = p_level->getLevelCamera().convert3DVectorWithScreenAlgorithm(wp3);
            sp4 = p_level->getLevelCamera().convert3DVectorWithScreenAlgorithm(wp4);


            //p_level->getLevelCamera();
        }

        void draw()//const
        {
            Moot::Color tempColor;

            windowInstance().graphics().drawQuad( sp1.x, sp1.y, tempColor,
                                                                                sp2.x,sp2.y, tempColor,
                                                                                sp3.x, sp3.y, tempColor,
                                                                                sp4.x,sp4.y, tempColor, 1);
        }


        void setLevel(Level* setLevel) {
            p_level = setLevel;
        }

    };
}

#endif

// Level.hpp

#ifndef GAME_LEVEL_HPP
#define GAME_LEVEL_HPP

#include <Moot/Forward.hpp>
#include <Moot/Window.hpp>
#include <Moot/Math.hpp>

#include <GameWindow.hpp>
#include <Camera.hpp>
#include <Corridor.hpp>
#include <Player.hpp>

#include <vector>


namespace Game
{
    class Level
    {

        typedef Corridor* p_corridor;
        typedef std::vector<p_corridor> CorridorList;
        typedef CorridorList::reverse_iterator ReverseCorridorItter;

        CorridorList m_map;
        Camera       m_camera;
        Player         m_player;


    public:

        Level()
        {
            m_player.setLevel(this);

            // Lots of vertices being defined into m_map.


            // Loop through and set camera
            ReverseCorridorItter rit;

            for(rit = m_map.rbegin(); rit != m_map.rend(); rit++)
                (*rit)->setLevel(this);


        }


        ~Level()
        {
            ReverseCorridorItter rit;

            for(rit = m_map.rbegin(); rit != m_map.rend(); rit++) 
                delete (*rit);

            m_map.clear();
        }


        void update() 
        {
            // Temp delete when input and player are implimented.
            if(pad[0].buttons & PAD_UP)
                m_camera.updateTargetOffsets(0, -2);

            if(pad[0].buttons & PAD_DOWN)
                m_camera.updateTargetOffsets(0, 2);

            if(pad[0].buttons & PAD_LEFT)
                m_camera.updateTargetOffsets(-2, 0);

            if(pad[0].buttons & PAD_RIGHT)
                m_camera.updateTargetOffsets(2, 0);

            m_player.update();


            ReverseCorridorItter rit;

            for (rit = m_map.rbegin(); rit != m_map.rend(); rit++)
                (*rit)->update();
        }


        void draw() // const // EH!!! wtf ReverseIter isn't a member
        {
            m_player.draw();


            ReverseCorridorItter rit;

            for (rit = m_map.rbegin(); rit != m_map.rend(); rit++)
                (*rit)->draw();

        }


        Camera& getLevelCamera() {
            return m_camera;
        }

    };
}

#endif

我尽可能设置指针,但是当我尝试从Level,BOOM访问函数时!

感谢。

PS:编译器是gcc 2.95.2,如果这有所不同。

修改

更新了完整的代码。

3 个答案:

答案 0 :(得分:7)

你是#include - Level的完整声明:

#include <Level.hpp>

... 然后您尝试转发声明Level

namespace Game
{
    class Level;

不要这样做。选择其中一个。 (编辑)或者至少将前向声明放在完整声明的#include之前。如果你在game_corridor.hpp中所做的只是设置指向Level的指针,那么前向声明应该没问题。但是,如果您需要在HPP文件中调用Level上的函数,那么您需要#include完整的声明。

EDIT2:

根据您对OP的澄清修改,必须 #include Level的完整声明,而不是尝试使用转发声明。

答案 1 :(得分:2)

如果您转发声明Game::Level,则不要#include它。在不太相关的说明中,请使用#include "header.hpp",而不是#include <header.hpp>

根据您的更新

编辑:将标头外的Game::Corridor::update()定义放入实施文件中。这样编译就不需要知道Game::Level的任何内容,除了它存在且它是一个类型。

答案 2 :(得分:0)

问题是Corridor不知道Level是什么,因为它不能真正#include Level.hpp,因为Level.hpp是#included Corridor.hpp。

潜在的问题是你正试图#include一个源文件。 真正的潜在问题是,当您没有将代码分离为源文件和头文件时,您正在使用#include。这是如何拆分它。 (我假设你熟悉将源文件编译成目标文件,然后将它们链接成可执行文件。)

Corridor.hpp:

#ifndef GAME_CORRIDOR_HPP
#define GAME_CORRIDOR_HPP

#include <Moot/Math.hpp>

#include <Level.hpp>

namespace Game
{
  class Level;

  class Corridor
  {
    static const unsigned int defaultLevelDepth = 800;

    Moot::Math::Vector3D wp1, wp2, wp3, wp4;
    Moot::Math::Vector2D sp1, sp2, sp3, sp4;

    Level * p_level;

  public:

    Corridor(Moot::Math::Vector3D setFirstPoint, Moot::Math::Vector3D setSecondPoint);

    void update();
    void draw();

    void setLevel(Level* setLevel);
  };
}

#endif

Corridor.cpp:

#include "Corridor.hpp"

namespace Game
{
  Corridor::Corridor(Moot::Math::Vector3D setFirstPoint, Moot::Math::Vector3D setSecondPoint) 
  {
    wp1 = setFirstPoint;
    wp2 = setSecondPoint;
    wp3 = setFirstPoint;
    wp3.z += defaultLevelDepth;
    wp4 = setSecondPoint;
    wp4.z += defaultLevelDepth;
  }

  void Corridor::update() 
  {  
    sp1 = p_level->getLevelCamera().convert3DVectorWithScreenAlgorithm(wp1);
    sp2 = p_level->getLevelCamera().convert3DVectorWithScreenAlgorithm(wp2);
    sp3 = p_level->getLevelCamera().convert3DVectorWithScreenAlgorithm(wp3);
    sp4 = p_level->getLevelCamera().convert3DVectorWithScreenAlgorithm(wp4);
  }

  // and so on

}