4 Replies - 1142 Views - Last Post: 21 June 2015 - 10:00 PM Rate Topic: -----

#1 rivers.coffman   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 21-June 15

Pure Virtual Function Errors

Posted 21 June 2015 - 07:20 PM

Hello, I am so close to finishing this assignment, but I am running into one last bit of code that has confused me to no end.
Background: we were given certain files that we needed to work around, one of which had a parent class and pure virtual function that we needed to use in multiple child classes. Everything compiled until it came to the main function. I ran into multiple errors that said "because the following virtual functions are pure within (child class)..." I do not know how to fix this error and all the forums that I have searched have been simply people not using const when declaring the pure virtual function in the header file. Here is some samples of my code (there are 10 files so I won't spam it unless it's needed).

#ifndef APPOINTMENT_H
#define APPOINTMENT_H

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

class Appointment
{
public:
   Appointment();
   Appointment(string desc, Time s, Time e);
   void print() const;
   virtual bool occurs_on(int year, int month, int day) const = 0;

private:
   string description;
   Time start;
   Time end;
};

#endif



//--------------------------------------------
// Appointment.cpp
//--------------------------------------------

#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include "Appointment.h"
#include "Time.h"

using namespace std;

Appointment::Appointment()
{
}

Appointment::Appointment(string desc, Time s, Time e)
{
  desc = description;
  s    = start;
  e    = end;
}

void Appointment::print() const
{
  Time s;
  Time e;
  cout << description << ": " << start.get_hours()
       << ":" << start.get_minutes() << " "
       << start.get_seconds() << " - " << end.get_hours()
       << ":" << end.get_minutes() << " "
       << end.get_seconds() << " " << endl;
}



#ifndef DATE_H
#define DATE_H

class Date
{
public:
   Date();
   Date(int y, int m, int d);
   void print() const;
   bool equals(Date other) const;
private:
   int day;
   int month;
   int year;
};

#endif


//-----------------------------------------
// Daily.cpp
//-----------------------------------------

#include <iomanip>
#include <iostream>
#include <string>
#include <cstring>
#include "Daily.h"

using namespace std;

Daily::Daily() : Date(1, 1, 1)
{
}

Daily::Daily(int y, int m, int d) : Date(y, m, d)
{
  y = y;
  m = m;
  d = d;
}


bool occurs_on(int year, int month, int day)
{
  if (day > 0) {return true;}
}



If you want all of the code to examine, I'll attach the entire project in a zip file.Attached File  p4.zip (5.8K)
Number of downloads: 69

Is This A Good Question/Topic? 0
  • +

Replies To: Pure Virtual Function Errors

#2 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6501
  • View blog
  • Posts: 22,256
  • Joined: 05-May 12

Re: Pure Virtual Function Errors

Posted 21 June 2015 - 08:02 PM

First, people on the other forums are correct in suggesting that you need to match the signatures. You have a couple of overrides which are missing the const.

Second, You have an error in your makefile:
#--------------------------------------------------------------
# p4 Makefile
#--------------------------------------------------------------
# Rivers Coffman
#--------------------------------------------------------------
# 21 June, 2015
#--------------------------------------------------------------

a.out: Appointment.o Monthly.o Onetime.o Daily.o Date.o Time.o main.o
	g++ Appointment.o Monthly.o Onetime.o Daily.o Date.o Time.o main.o

Time.o: Time.h Time.cpp
	g++ -c Time.cpp

Date.o: Date.h Date.cpp
	g++ -c Date.cpp

Appointment.o: Appointment.h Appointment.cpp
	g++ -c Appointment.cpp

Montly.o: Monthly.h Montly.cpp
	g++ -c Montly.cpp

Onetime.o: Onetime.h Onetime.cpp
	g++ -c Onetime.cpp

Daily.o: Daily.h Daily.cpp
	g++ -c Daily.cpp

main.o: Appointment.h Monthly.h Onetime.h Daily.h Date.h Time.h main.cpp
	g++ -c main.cpp

clean:
	/bin/rm -f *.o a.out *~

#--------------------------------------------------------------



Note the typo on line 21.

Since your final link commands for line 10 includes a non-existent file. The linker can't find an implementation of a pure virtual function.
Was This Post Helpful? 0
  • +
  • -

#3 rivers.coffman   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 21-June 15

Re: Pure Virtual Function Errors

Posted 21 June 2015 - 08:43 PM

View PostSkydiver, on 21 June 2015 - 08:02 PM, said:

First, people on the other forums are correct in suggesting that you need to match the signatures. You have a couple of overrides which are missing the const.


Ok so I fixed the typo (stupid me) but when I try to use const for my derived member functions of the pure virtual function, it says that I cannot use cv-qualifiers, so I removed them. Now I am reading an error message that says "Cannot allocate an object of abstract type." What does that mean? Is it something about my functions or is it in logic/syntax that I have? I looked at a few forums and questions similar but ran up empty since the majority of the issues I saw were already resolved...
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6501
  • View blog
  • Posts: 22,256
  • Joined: 05-May 12

Re: Pure Virtual Function Errors

Posted 21 June 2015 - 09:54 PM

Post your new code. Preferably post the actual code instead of offering up a .zip for download.
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6501
  • View blog
  • Posts: 22,256
  • Joined: 05-May 12

Re: Pure Virtual Function Errors

Posted 21 June 2015 - 10:00 PM

View Postrivers.coffman, on 21 June 2015 - 11:43 PM, said:

Ok so I fixed the typo (stupid me) but when I try to use const for my derived member functions of the pure virtual function, it says that I cannot use cv-qualifiers, so I removed them.

If that is true, why didn't you tell us that you were getting that error with regards to the Daily.h that you posted in your original post? That header file had the const qualifier for the your override of the virtual function.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1