Hi all,
I am trying to write a C++ program to create a simple text-based interface that should print out the relevant stubs when a certain choice is made. I am however getting linking errors such as-->[Linker error] undefined reference to `Database::AddStudent()' , that i cant seem to fix. Any help is greatly appreciated. I am using Dev-C++ 4.9.9.2 Please see my pieces of code below:-

Driver.cpp:

#include "Database.h"
#include <iostream>
#include <cstdlib>

using namespace std;
  int main(int argc, char * argv[])
  {
      
	Database Data;
    	char k;
  do
  {
    cout<<"Enter a number(0-7) or q to quit"<<endl;
    cin >>k;
    switch(k)
      {
        case'0':
                Data.AddStudent();
		break;
	case'1':
		Data.DeleteStudent();
		break;
	case'2':
		Data.ReadDatabase();
		break;
	case'3':
		Data.SaveDatabase();
		break;
	case'4':
		Data.DisplayDetails();
		break;
	case'5':
		Data.GradeStudent();
		break;
	case'6':
		Data.DisplayAll();
		break;
	default:
		std:: cout<<"Invalid entry!!"<<endl;
		break;
      }

    }while(k!='q');

  system("PAUSE");
  return 0;
  };

Database.cpp:

#include <stdio.h>
#include <iostream>
#include <cstdlib>
#include "Database.h"

using namespace std;

	Database::Database()
	{
	  cout << "Created TestClass\n";
	}

	Database::~Database() //Deconstructor
	{
	}
	
   //Methods to do the required data manipulations
    public:
    void Database::AddStudent(void) //Enter new student data
      {
	     cout<<"AddStudent() called"<<endl;
      }

   void DeleteStudent(void) //delete existing student record based on the given student number****
      {
	    cout<<"DeleteStudent() called"<<endl;
      }
      
    void ReadDatabase(void) //read a simple text file which stores a list of database entries
      {
 	    cout<<"ReadDatabase() called"<<endl;
      }
      
    void SaveDatabase(void) //Write to a simple text file which stores a list of database entries
      {
	     cout<<"SavaDatabase() called"<<endl;
      }
      
    void GradeStudent(void)//Print an average for a student based on a given student number. The average= number in the ClassRecord string and averaging
      {
	     cout<<"GradeStudent() called"<<endl;
      }
      
    void DisplayDetails(void) //Print the record for a given student, based on the student number entered 
      {
	     cout<<"DisplayDetails() called"<<endl;
      }
      
    void DisplayAll(void) //Print records for all students in the database
      {
	     cout<<"DisplayAll() called"<<endl;
      }

Database.h:

#ifndef DATABASE_H
#define DATABASE_H
using namespace std;

class Database
{

  public:
	
	//Database();
	virtual ~Database();

	void AddStudent();
	
	void DeleteStudent();
	
	void ReadDatabase();
	
	void SaveDatabase();
	
	void GradeStudent();

	void DisplayDetails();

	void DisplayAll();

};

#endif

>> I am however getting linking errors such as-->[Linker error] undefined reference to `Database::AddStudent()'

That's actually the one error you SHOULDN'T get. Are you sure you're getting it? Look at AddStudent. That's the one function that is correct, so model the others after it. The rest of them are wrong. AddStudent has the required "Database::" in front of it in the cpp file. The rest of them are missing that.

Delete line 18 in the cpp file. "public" goes in the .h file, not in the cpp file, and you already have it in the .h file.

>> I am however getting linking errors such as-->[Linker error] undefined reference to `Database::AddStudent()'

That's actually the one error you SHOULDN'T get. Are you sure you're getting it? Look at AddStudent. That's the one function that is correct, so model the others after it. The rest of them are wrong. AddStudent has the required "Database::" in front of it in the cpp file. The rest of them are missing that.

Delete line 18 in the cpp file. "public" goes in the .h file, not in the cpp file, and you already have it in the .h file.

I just did the above: And now and getting Linker errors for all the functions; including the constructors i.e [Linker error] undefined reference to `Database::~Database()' . there's two other errors, maybe they are the cause of the problem, In function `ZSt17__verify_groupingPKcjRKSs': and [Linker error] undefined reference to `vtable for Database'

Just used a make file and compiled it via the terminal in Ubuntu and it works perfectly! I guess Windows/Dev-C++ was the problem. Thank you....

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.