简体   繁体   English

C ++前向声明错误

[英]C++ forward declaration error

I have an error that goes like this 我有一个像这样的错误

    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 and Level are ... 走廊和水平......

  // 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

and

// 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

The pointer is being set as far as I can tell, but when I try to access a function from Level, BOOM! 据我所知,指针正在设置,但是当我尝试从Level,BOOM访问一个函数时!

Thanks. 谢谢。

PS: The compiler is gcc 2.95.2 if that makes a difference. PS:编译器是gcc 2.95.2,如果这有所不同。

EDIT 编辑

Updated with complete code. 更新完整代码。

You are #include -ing Level 's complete declaration: 你是#include -ing Level的完整声明:

#include <Level.hpp>

...and then you try to forward-declare Level : ... 然后你尝试向前声明Level

namespace Game
{
    class Level;

Don't do this. 不要这样做。 Choose one or the other. 选择其中一个。 ( edit ) Or at least put the forward-declaration before the #include -ion of the complete declaration. 编辑 )或者至少在完整声明的#include -ion之前放置前向声明。 If all you're doing in game_corridor.hpp is setting pointers to a Level , then a forward declare should do fine. 如果你在game_corridor.hppgame_corridor.hpp只是设置指向Level指针,那么前向声明应该没问题。 If however you need to call functions on Level from within the HPP file, then you'll need to #include the complete declaration. 但是,如果您需要在HPP文件中调用Level函数,那么您需要#include完整的声明。

EDIT2: EDIT2:

Based on your clarifying edit to your OP, you must #include the complete declaration of Level , and not try to use a forward declaration. 根据您对OP的澄清编辑,您必须 #include完整的Level声明,而不是尝试使用前向声明。

If you forward-declare Game::Level then don't #include it. 如果你转发声明Game::Level那么就不要#include它。 In a not-so-related note, use #include "header.hpp" , not #include <header.hpp> . 在一个不那么相关的注释中,使用#include "header.hpp" ,而不是#include <header.hpp>

Edit as per your updates: Bring the definition of Game::Corridor::update() outside the header and into an implementation file. 根据您的更新进行编辑 :将标题Game::Corridor::update()置于标题之外,并放入实现文件中。 This way the compile need not know anything about Game::Level apart from the fact that it exists and it's a type. 这样编译就不需要了解Game::Level除了它存在并且它是一个类型。

The problem is that Corridor doesn't know what a Level is, because it can't really #include Level.hpp, because Level.hpp is what #included Corridor.hpp. 问题是Corridor不知道Level是什么,因为它不能真正#include Level.hpp,因为Level.hpp是#included Corridor.hpp。

The underlying problem is that you're trying to #include a source file. 根本问题是你正试图#include一个源文件。 The really underlying problem is that you're using #include when you haven't separated your code into source files and header files. 真正潜在的问题是,当您没有将代码分离为源文件和头文件时,您正在使用#include Here's how to split it up. 这是如何拆分它。 (I'm assuming you're familiar with compiling source files into object files, then linking them into executables.) (我假设您熟悉将源文件编译为目标文件,然后将它们链接到可执行文件中。)

Corridor.hpp: 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: 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

}

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

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