0

(Edited Title to Reflect the Cause of the Problem)

OLD TITLE: Undefined symbols for architecture x86_64: (C++) - What is it if my constructor & member functions are correct?

Working on a project for class and wanted to first verify the code from the text book but it doesn't working so I can't proceed onto the assignment.

The other posts I've read on this topic either point to a problem with defining the content in the constructor body or a problem where the class was not included in the definition of the member function (i.e. GradeBook:: GradeBook() , in my case).

The files I have are:

  • GradeBook.h
  • GradeBook.cpp
  • main.cpp

When I run a g++ main.cpp I get the following:

Undefined symbols for architecture x86_64:
  "GradeBook::GradeBook(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)", referenced from:
    _main in main-3ed012.o
  "GradeBook::getCourseName() const", referenced from:
    _main in main-3ed012.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

My files:

GradeBook.h

// GradeBook.h

#include <string>

class GradeBook  // GradeBook class definition
{
public:
    explicit GradeBook( std::string ); // constructor initialize courseName
    void setCourseName( std::string ); // sets the course name
    std::string getCourseName() const; // gets the course name
    void displayMessage() const; // displays a welcome message
private:
    std::string courseName; // course name for this GradeBook
}; // end class GradeBook

GradeBook.cpp

// GradeBook.cpp

#include <iostream>
// include definition of class GradeBook
#include "GradeBook.h"

using namespace std;

// constructor initializes courseName with string supplied as argument
GradeBook::GradeBook( string name )
   : courseName( name ) // member initializer to initialize courseName
{
        // empty body
}  // end GradeBook constructor

// function to set the course name
void GradeBook::setCourseName( string name )
{
    courseName = name; // store the course name in the object
} // end function setCourseName

// function to get the course name
string GradeBook::getCourseName() const
{
    return courseName; // return object's courseName
} // end function getCourseName

// display a welcome message to the GradeBook user
void GradeBook::displayMessage() const
{
    // call getCourseName to get the courseName
    cout << "Welcome to the grade book for\n" << getCourseName()
            << "!" << endl;
}  // end function displayMessage

main.cpp

// main.cpp

#include <iostream>
#include "GradeBook.h"
using namespace std;

// function main begins program execution
int main()
{
    // create two GradeBook objects
    GradeBook gradeBook1( "CS101 Instruction to C++ Programming" );
    GradeBook gradeBook2( "CS102 Data Structures in C++" );

    // display initial value of courseName for each GradeBook
    cout << "gradeBook1 created for course: " << gradeBook1.getCourseName()
            << "\ngradeBook2 created for course: " << gradeBook2.getCourseName()
            << endl;
} // end main

Thanks ahead of time for any help!

  • Am I missing something I'm supposed to include in the process? I don't have a GradeBook.o file. – satelliteDave Oct 26 '15 at 05:43
  • The only thing that I did for compilation & linking was ensure all the files are in the same folder and type `g++ main.cpp` – satelliteDave Oct 26 '15 at 05:49
  • There you go then. You have to compile `GradeBook.cpp` too. The compiler doesn't magically check all files in the directory or something. – M.M Oct 26 '15 at 06:32
  • Okay, Thanks M.M! I ran `g++ GradeBook.cpp` and it spit out: Undefined symbols for architecture x86_64: "_main", referenced from: implicit entry/start for main executable ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) – satelliteDave Oct 26 '15 at 06:48
  • You have to compile both, not just one or the other. For example `g++ main.cpp GradeBook.cpp` – M.M Oct 26 '15 at 06:54
  • THAT WAS IT! Thanks M.M! Such a simple answer but I had no idea what I was doing wrong. – satelliteDave Oct 26 '15 at 15:39

0 Answers0