I’m trying to create a method in a C++ class that can be called without creating an instance of the class (like a static method in Java), but I keep running into this error: error: expected unqualified-id before ‘.’ token
Here’s the .cpp file I’m trying to compile:
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;
}
In C++ it’s
and you have to declare the method as
static.::is called the scope resolution operator. You can call the method with.if it’s on a class instance, but the instance is not required (it being static and all…).