I'm trying to write a program in C++ using Atom Editor on Ubuntu 18.04. I'm very new to using Linux and all my previous experience coding is on computers and IDEs that were already set up for me to use so this is hopefully a simple fix. As far as I'm aware I have installed GCC correctly. Executing which gcc g++
in Terminal returns
/usr/bin/gcc
/usr/bin/g++
and I'm using the gpp-compiler package with Atom to compile and run my programs within Atom.
My program currently consists of header file "Creature.h" with the constructor and methods
#ifndef CREATURE_H
#define CREATURE_H
class Creature {
private:
int fHealth;
public:
Creature(int Health);
virtual ~Creature(){}
void setHealth(int Health){fHealth = Health;}
int getHealth(){return fHealth}
};
#endif
with the constructor defined in the implementation file "Creature.cpp":
#include "Creature.h"
Creature::Creature(int Health){
setHealth(Health);
}
When I then create an instance of this class in my main program, e.g.
#include "iostream"
#include "Creature.h"
using namespace std;
int main(){
Creature MyCreature(10);
cout<<MyCreature.getHealth()<<endl;
return 0;
}
and try to compile and run my program I get the error
/tmp/cc4H4p2l.o: In function `main':
<br/>main.cpp:(.text+0x47): undefined reference to `Creature::Creature(int)'
collect2:error: ld returned 1 exit status
From my limited understanding and the investigation I've done it seems to me that either the Creature.cpp file hasn't been compiled, or the resulting object files are not linked.
In the first case, I am not sure how to compile just the Creature.cpp file as if I try to do this within Atom it also tries to run the file and fails as obviously there is no main function. I also tried to do this in terminal by executing g++ -c Creature.cpp
and still get the same error.
In the second case, I'm not really sure what this means or how to do it.
Thanks in advance for any help I'm able to get.