0

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?

Anthony Krivonos
  • 4,596
  • 4
  • 16
  • 31
  • 2
    Is the `Name.cpp` file built and linked? Is it possible to see the actual build log in the IDE? – Some programmer dude Feb 15 '17 at 15:16
  • What you showed us does not produce the error you mentioned. It results in three other errors when trying to compile. Please provide a [MCVE](http://stackoverflow.com/help/mcve). –  Feb 15 '17 at 15:21
  • The issue ended up being the compiler. Cloud9 did not automatically reference Name.h, Name.cpp, and Name_Main.cpp, and thus I needed to write the following code: `g++ -o exec Name.h Name.cpp Name_Main.cpp` I also tried compiling and running the files in a new XCode target, and it seemed to work just fine without the above line. – Anthony Krivonos Feb 15 '17 at 16:01

0 Answers0