编译错误:在"&"标记之前应为")"

Compilation Error : expected ‘)’ before ‘&’ token

本文关键字:错误 编译      更新时间:2023-10-16

这是我真的不明白的事情:我没有看到任何实际上使这个错误容易发生的事情。

这是类:

namespace Engine_Main {
class SceneManager
{
public:
    SceneManager(Engine& engine);
    void createScene();
private:
    Ogre::SceneManager * mSceneMgr;
};

}

以及其他一些类供参考:

#ifndef ENGINE_H
#define ENGINE_H
#include <OGRE/OgreSceneManager.h>
#include <OGRE/OgreRoot.h>
#include "scenemanager.h"
#include "playerinput.h"
namespace Engine_Main {
class Engine
{
public:
    Engine();
    ~Engine();
    void initGameLoop();
    PlayerInput * getPlayerInput();
    PlayerMovement * getPlayerMovement();
    Ogre::Root * getOgreRoot();
private:
    //fields
    PlayerInput * mPInput;
    PlayerMovement * mPMovement;
    Ogre::Root * mRoot;
    //methods
    void registerInput();
    void createScene();
    void renderPosition();
};
}
#endif // ENGINE_H
#include "engine.h"

namespace Engine_Main {
    /**********/
    /* PUBLIC */
    /**********/
    PlayerMovement * Engine::getPlayerMovement() {
        return mPMovement;
    }
    PlayerInput * Engine::getPlayerInput() {
        return mPInput;
    }
    Engine::Engine() {
        mPInput = new PlayerInput();
        mPMovement = new PlayerMovement();
        mRoot = new Ogre::Root("cfg/plugins.cfg", "cfg/engine.cfg", "cfg/engine.log");
    }
    Engine::~Engine(){
        if (mPInput) {
            delete mPInput;
        }
        if (mRoot) {
            delete mRoot;
        }
    }
    void Engine::createScene() {
    }
}

我的问题

我做错了什么?

您是否在"scenemanager.h"中缺少Engine的(前向)声明?当编译器解析:

...
SceneManager(Engine&);
...

需要声明Engine类型。在声明SceneManager类之前,您可能需要像class Engine;这样的前向声明。