‘initPhysics’ was not declared in this scope.
I have simplified my program as much as I could; here it is:
helloworld.hpp
#ifndef HELLOWORLD_HPP
#define HELLOWORLD_HPP
class testWorld {
public:
testWorld() {}
~testWorld() {}
void initPhysics();
};
#endif
hello.cpp
#include "helloworld.hpp"
#include <iostream>
using namespace std;
void testWorld::initPhysics() {
cout << "Initiating physics..." << endl;
}
int main(int argc,char** argv) {
cout << "Hello World!" << endl;
testWorld* world;
world = new testWorld();
world<-initPhysics();
return 0;
}
I compile with the command
g++ -c hello.cpp
and get the error
hello.cpp: In function ‘int main(int, char**)’:
hello.cpp:14:21: error: ‘initPhysics’ was not declared in this scope
Why doesn’t the compiler see the declaration of initPhysics, even though I included helloworld.hpp?
It should be
world->initPhysics(), notworld<-initPhysics()Your version is being read as the expression “world is less than -1 multiplied by the result of the global function initPhysics()” and it’s that global function that it can’t find.
And although this is obviously test code, I’d just like to point out that if you allocate an object with
new, you must explicitlydeleteit somewhere.