可能的转发声明错误

Possible forward declaration error

本文关键字:声明 错误 转发      更新时间:2023-10-16

我有两个文件。一个是main.cpp它 #includes"MouseController.h",它有一个鼠标对象的classdef。如果 MouseController.h 没有导入或包含,为什么 main 在查找我的鼠标类时遇到问题?

如果它有任何用途,Xcode 的 CMD 单击允许您将某些内容追溯到其声明,它会发现 Mouse 类正常。

主.cpp

#include <GLUT/GLUT.h>
#include <OpenGL/OpenGL.h>
#include <iostream>
#import "MouseController.h"
Mouse mouse;
float angle = 0.0f;
void selfInit();
void glInit();
void DisplayCallback();
void ReshapeCallback(int w, int h);
void UpdateAll(int value);

int main(int argc, char** argv)
{
    selfInit();
    glutInit(&argc, argv);
    glInit();
    return 0;
}

此处之后不再引用鼠标对象。

鼠标控制器.h

#ifndef __FinalPiece__MouseController__
#define __FinalPiece__MouseController__
class Mouse {
    public:
    static int CurrentX;
    static int CurrentY;
    static int LastX;
    static int LastY;
    static int DeltaX;
    static int DeltaY;
    static void move(int x, int y);
    static void update();
    static void clicked(int button, int state, int x, int y);
};
#endif

没有看到你的代码,这是一个黑暗中的镜头,但听起来你遇到了链接器错误。

// main.cpp
#include "MouseController.h"
int main()
{
    // do stuff
    return 0;
}
// MouseController.cpp
#include "MouseController.h"
// implement MouseController here
// MouseController.h
// define MouseController here
如果 MouseController

.cpp 不存在,未被编译(即不是项目或 Makefile 的一部分),或者 MouseController.h 没有内联实现类,编译器知道 MouseController 应该是什么样子,但链接器找不到它。