c++interfaceg++

C++ Interface Compiling


EDIT:

I figured out the solution. I was not adding -combine to my compile instructions and that was generating the errors.


I'm in the process of working through the Deitel and Deitel book C++ How to Program and have hit a problem with building and compiling a C++ interface using g++. The problem is, I've declared the class in the .h file and defined the implementation in the .cpp file but I can't figure out how to get it to compile and work when I try to compile the test file I wrote. The g++ error I'm receiving is:

Undefined symbols:
  "GradeBook::GradeBook(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)", referenced from:
      _main in ccohy7fS.o
      _main in ccohy7fS.o
  "GradeBook::getCourseName()", referenced from:
      _main in ccohy7fS.o
      _main in ccohy7fS.o
ld: symbol(s) not found
collect2: ld returned 1 exit status<

If someone could point me in the right direction I'd be appreciative.

My header file:


//Gradebook 6 Header
//Purpose is to be the class declaration for the class Gradebook 6
//Declare public, privates, and function names. 

#include  //the standard c++ string class library
using std::string;

//define the class gradebook
class GradeBook
{
    public:  //all the public functions in the class

     GradeBook(string ); //constructor expects string input
     void setCourseName (string ); //method sets course name--needs string input
     string getCourseName(); //function returns a string value
     void displayMessage();  //to console

    private: //all private members of the class
        string courseName; 
}; //ends the class declaration 

My .cpp file is:


//Gradebook 6
// The actual implementation of the class delcaration in gradebook6.h

#include 
using std::cout;
using std::endl;

#include "gradebook6.h" //include the class definition

//define the class gradebook

GradeBook::GradeBook(string name) //constructor expects string input
{
    setCourseName(name); //call the set method and pass the input from the constructor. 
}

void GradeBook::setCourseName (string name) //method sets course name--needs string input
{
    courseName = name; //sets the private variable courseName to the value passed by name
}

string GradeBook::getCourseName() //function returns a string value
{
    return courseName;
}

void GradeBook::displayMessage()  //function does not return anything but displays //message to console
{
   cout //message here, the pre tag isn't letting it display
} //end function displayMessage

Finally, the test file I wrote to implement the interface and test it.


// Gradebook6 Test
// Program's purpose is to test our GradeBook5 header file and file seperated classes

#include 
using std::cout;
using std::endl;

#include "gradebook6.h" //including our gradebook header from the local file.

//being program
int main()
{
    //create two gradebook objects 
    GradeBook myGradeBook1 ("CSC 101 Intro to C++ Programming"); //create a default object using the default constructor
    GradeBook myGradeBook2 ("CSC 102 Data Structures in C++");

    //display intitial course name
    cout //another output message here that the code tag does not like

    return 0;
}

Solution

  • Looks like you just need to link in the GradeBook.cpp object file to your final executable. Care to post your makefile or the way you are building it?