I'm using cygwin g++ to compile my code and Notepad++ as my editor.
The code i authored works fine if i implement member functions in the class definition .h file.

However g++ throws a chunk of gibberish at me when i replace member functions with function prototypes in the class definition .h file and place the member functions in a separate .cpp .

Here is the error output i get:

g++     main.cpp   -o main
/tmp/ccDvfvB7.o:main.cpp:(.text+0xc1): undefined reference to `GradeBook::GradeB
ook(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/tmp/ccDvfvB7.o:main.cpp:(.text+0x10a): undefined reference to `GradeBook::getDa
ta()'
/tmp/ccDvfvB7.o:main.cpp:(.text+0x230): undefined reference to `GradeBook::setDa
ta(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/tmp/ccDvfvB7.o:main.cpp:(.text+0x271): undefined reference to `GradeBook::getDa
ta()'
collect2: ld returned 1 exit status
make: *** [main] Error 1

Main.cpp

// Classes Practical
// Author : -

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

int main(){

    GradeBook myObject( "C++ : How To Program by Deitel & Deitel" );
    cout << "\nGrade book object created and contains : \n"
    << myObject.getData() << ".\n" << endl;
    
    string variable;
    cout << "Please assign new data to Grade book object." << endl;
    getline( cin, variable );
    
    myObject.setData( variable );
    cout << "\nGrade book object now contains :\n"
    << myObject.getData() << endl;
    
    return 0;

GradeBook.h

//GradeBook Class Definition
// Author : -
#include <iostream>
#include <string>
using namespace std;

class GradeBook{

public:
    //Constructor
    GradeBook( string );
    
    //Member Functions
    string getData();
    void setData( string );    
    void displayData();
    
private:
    string data;
};

GradeBook.cpp

// GradeBook Class Implementation
// Author : -
#include <iostream>
#include "GradeBook.h"
using namespace std;

//Constructor
GradeBook::GradeBook( string parameter ){
    setData( parameter );
}

//Member Functions
void GradeBook::setData( string parameter ){
    data = parameter;
}

string GradeBook::getData(){
    return data;
}

void GradeBook::displayData(){
    cout << "Data member \"Data\" now contains : " << getData() << endl;
}

Thank you for your time.

You need to compile GradeBook.cpp as well. Only headers need not be compiled:

g++ main.cpp GradeBook.cpp -o main
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.