For my second semester of an introductory class for C++, I made a class called Name.h
that contains the following information:
#ifndef NAME_H
#define NAME_H
class Name
{
private:
string firstName;
string lastName;
string middleInitial;
public:
Name();
Name(string, string, string);
void setName(string, string, string);
void setFirstName(string);
void setLastName(string);
void setMiddleInitial(string);
void display() const;
string getFirstName() const {
return firstName;
}
string getLastName() const {
return lastName;
}
string getMiddleInitial() const {
return middleInitial;
}
};
#endif
The implementation is provided in the file Name.cpp
as shown below:
#include <iostream>
#include <string>
using namespace std;
#include "Name.h"
Name::Name() {}
Name::Name(string firstName, string lastName, string middleInitial) {
this->firstName = firstName;
this->lastName = lastName;
this->middleInitial = middleInitial;
}
void Name::setName(string firstName, string lastName, string middleInitial) {
this->firstName = firstName;
this->lastName = lastName;
this->middleInitial = middleInitial;
}
void Name::setFirstName(string firstName) {
this->firstName = firstName;
}
void Name::setLastName(string lastName) {
this->lastName = lastName;
}
void Name::setMiddleInitial(string middleInitial) {
this->middleInitial = middleInitial;
}
void Name::display() {
cout << firstName << " " << middleInitial << " " << lastName << endl;
}
Lastly, I tried testing the appropriate function and constructor implementations in the Name_Main.cpp
file below:
#include <iostream>
#include <string>
using namespace std;
#include "Name.h"
int main() {
string firstName;
string lastName;
string middleInitial;
cout << "Using constructor: " << endl;
cout << "Enter first name: ";
cin >> firstName;
cout << "Enter last name: ";
cin >> lastName;
cout << "Enter middle initial: ";
cin >> middleInitial;
Name name(firstName, lastName, middleInitial);
cout << "Date: ";
name.display();
cout << "Using setName(): " << endl;;
cout << "Enter first name: ";
cin >> firstName;
cout << "Enter last name: ";
cin >> lastName;
cout << "Enter middle initial: ";
cin >> middleInitial;
name.setName(firstName, lastName, middleInitial);
cout << "Date: ";
name.display();
cout << "Using setters: " << endl;
cout << "Enter first name: ";
cin >> firstName;
cout << "Enter last name: ";
cin >> lastName;
cout << "Enter middle initial: ";
cin >> middleInitial;
name.setFirstName(firstName);
name.setLastName(lastName);
name.setMiddleInitial(middleInitial);
cout << "Date: ";
name.display();
return 0;
}
After trying to test out the class functions and constructors in Name_Main.cpp
using the Cloud9 IDE, I kept receiving the following error:
/tmp/cccNHMnU.o: In function `main':
Name_Main.cpp:(.text+0x126): undefined reference to `Name::Name(std::string, std::string, std::string)'
It appears Name_Main.cpp
cannot find the appropriate methods in Name.h
and their implementations in Name.cpp
. I tried changing all of the string
variable types to std::string
variable types in Name.h
, as well as adding using namespace std;
and #include <string>
into the header file, to no avail. Does anyone know why I am receiving this error, or has recommendations for solving this issue?