C++ Error: Undefined Reference When Initializing a Class Object

C++ Error: Undefined Reference When Initializing a Class Object

这是我进入 C++ 世界的第一步。我正在试验一个使用 class 的简单程序。该程序提示用户输入用户名和年龄,然后用给定的数据创建一个 class 对象,然后 return 接受 Person 的 class 对象和 return是一句问候。但是,当我尝试构建程序时出现以下错误:

"C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin\g++.exe" -g "D:\OOP Labs and Assignments\Lab5\lab5.cpp" -o "D:\OOP Labs and Assignments\Lab5\lab5.exe" C:\Users\okii_\AppData\Local\Temp\cc1mDRfr.o: In function main': D:/OOP Labs and Assignments/Lab5/lab5.cpp:21: undefined reference to Person::Person(std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&, int)' collect2.exe: error: ld returned 1 exit status

Build finished with error(s). The terminal process failed to launch (exit code: -1).

我将此追溯到错误发生的地方,它似乎是在我尝试创建对象时发生的。我不知道为什么会收到此错误。任何指导将不胜感激。我正在使用 Visual Studio 代码和 g++ 来编译我的代码,我的文件如下。

Person.hpp:

#ifndef PERSON_HPP
#define PERSON_HPP
#include <string>

class Person 
{
    private:
        // private section: private class members will be added here
        std::string name;
        int age;
    
    public:
        //pulic section: public class memeber will be added here
        // Constructor
        // Parameter theName: the name of the person
        // theAge: the age of the person
        Person(const std::string & theName, int theAge);

        // Get the name of the person, returned as a constant reference.
        const std::string & getName() const;

        // Get the age of the person
        int getAge() const;

        // Set age of the person
        void setAge(int value)
        {
            this->age = value;        
        }
};

#endif

Person.cpp:

#include "Person.hpp"
using namespace std;

Person::Person(const string & theName, int theAge)
    : name{theName}, age{theAge}
{} // empty body constructor

const string & Person::getName() const
{
    return name;
}

int Person::getAge() const
{
    return age;
}

lab5.cpp

#include <iostream>
#include "Person.hpp"
using namespace std;

void greet(const Person & p);

int main()
{
   string userName;
   int userAge;

   cout << "Enter your name: ";
   cin >> userName;
   cout << "Enter your age: ";
   cin >> userAge;

   Person thePerson(userName, userAge);
   greet(thePerson);
}

void greet(const Person & p)
{
   string name = p.getName();
   int age = p.getAge();

   cout << "Hello " << name;
   cout << "Your age is " << age;
}

tasks.json:

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe build active file",
            "command": "C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin\g++.exe",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "compiler: \"C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin\g++.exe\""
        }
    ]
}

正如 Drew 和 Useless 所指出的,我需要先编译我的程序才能解决错误。我编译了程序,运行很顺利。