对实现文件中定义的构造函数的未定义引用

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++

并且我将gpp-compiler软件包与Atom一起使用,以在Atom中编译和运行程序。

我的程序当前由头文件“ Creature.h”以及构造函数和方法组成

#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

在实现文件“ Creature.cpp”中定义的构造函数:

#include "Creature.h"

Creature::Creature(int Health){
    setHealth(Health);
}

然后在主程序中创建此类的实例时,例如

#include "iostream"
#include "Creature.h"

using namespace std;

int main(){

    Creature MyCreature(10);

    cout<<MyCreature.getHealth()<<endl;

    return 0;
}

并尝试编译并运行我的程序我得到了错误

/tmp/cc4H4p2l.o: In function `main':
<br/>main.cpp:(.text+0x47): undefined reference to `Creature::Creature(int)'
collect2:error: ld returned 1 exit status

从我有限的理解和调查中,我认为Creature.cpp文件尚未编译,或者结果对象文件未链接。

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.

在第二种情况下,我不太确定这意味着什么或如何做。

在此先感谢您能获得的任何帮助。

评论