This is my first step into the world of C++. I am experimenting with a simple program that is using a class. The program is to prompt the user for a username and age, then create a class object with the given data and then return the greet method which takes in the class object of Person and returns a greeting. However, when I try to build to program I get the following error:
"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 toPerson::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).
I traced this back to where the error occurs and it seems to be happening when I try to create an object. I am at a loss as to why I am getting this error. Any guidance would be appreciated. I am using Visual Studio Code and g++ to compile my code and my files are below.
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\""
}
]
}
Person::Person(const string &, int)is defined in the file Person.cpp. That file certainly exists somewhere on your computer, but it is not being compiled and linked as part of your project.lab5.cppdirectly intolab5.exe, without compilingPerson.cppat all. Typically C++ compilation has two stages: the first would be building the object fileslab5.o(or.objor whatever) andPerson.o. The second stage, which you're jumping straight to, links those files into an executable.