错误 C2065:未声明的标识符,即使声明了函数

Error C2065: undeclared identifier, even when the function is declared

我试图在 myClass.cpp 中调用一个函数 randomFunction(),但我得到

error C2065: randomFunction: undeclared identifier

randomFunction()anotherClass.h 中声明,我在 myClass.h 中包含 #include "anotherClass.h" 但仍然出现此错误。

我该如何解决这个问题?

如果你有 randomFunction() static:

class CAnotherClass
{
public:
    static void randomFunction();
};

然后,randomFunction() 可以在不创建 CAnotherClass 对象的情况下调用 ,使用此语法:

CAnotherClass::randomFunction();

如果你randomFunction() 不是 static:

class CAnotherClass
{
public:
    void randomFunction();
};

然后,randomFunction() 不能 在没有创建 CAnotherClass 对象的情况下被调用,然后你必须使用这个语法:

CAnotherClass myInstance;
myInstance.randomFunction();

或:

CAnotherClass().randomFunction(); // temporary object creation

请注意,如果 randomFunction()static,上述两种语法也适用。

PS:我试图在没有源代码的情况下猜测问题所在....希望这会有所帮助!