I have a a Class (MethodClass.h & MethodClass.cpp files) and a main.cpp
In main I'm calling the constructor and then a method.
Constructor is working fine but for the method I get the Error: "Test/main.cpp:13: undefined reference to `MethodClass::testMethod()'"
I simplified the problem with this test-project:
MethodClass.h
#ifndef METHODCLASS_H
#define METHODCLASS_H
#include <cstdlib>
#include <iostream>
class MethodClass {
public:
MethodClass();
MethodClass(const MethodClass& orig);
virtual ~MethodClass();
void testMethod();
private:
};
#endif /* METHODCLASS_H */
MethodClass.cpp:
#include "MethodClass.h"
using namespace std;
MethodClass::MethodClass() {
cout << "Constructor: MethodClass" << endl;
}
MethodClass::MethodClass(const MethodClass& orig) {}
MethodClass::~MethodClass() {}
void testMethod(){
cout << "testMethod" << endl;
}
main.cpp:
#include <cstdlib>
#include "MethodClass.h"
#include "MethodClass.h"
using namespace std;
int main(int argc, char** argv) {
MethodClass mClass = MethodClass();
cout << "hallo" << endl;
mClass.testMethod();
return 0;
}
- The problem is with the last line in the main.cpp: mClass.testMethod();
If i rem this, the constructor works fine - result : Constructor: MethodClass hallo
- Also very strange I don't even have to include the "MethodClass.h" in the main.cpp...
If I rem the first line as well: //#include "MethodClass.h", it's still fine, is this normal or can you explain to me why this works?
Btw: I'm Using Netbeans 8.0.2 with MinGW Compiler