我正在尝试在C++类中创建一个方法,该方法可以在不创建类的实例的情况下被调用(就像Java中的静态方法),但我一直遇到这个错误:
error: expected unqualified-id before ‘.’ token
这是我正在尝试编译的.cpp文件:using namespace std;
#include <iostream>
class Method {
public:
void printStuff(void) {
cout << "hahaha!";
}
};
int main(void){
Method.printStuff(); // this doesn't work as expected!
return 0;
}
static
成员函数是语言的基本特性。 - David Rodríguez - dribeas