Hi Stack Over Flow Users!
Need some help with a pure virtual function. I have googled the error that I get: "undefined reference to 'vtable for Sphere'" and read other posts here at stack overflow. What I gather is that the linker doesn't know where the body of the function is...
Complete error message: /tmp/ccXEIJAQ.o:main.cpp(.rdata$.refptr._ZTV6Sphere[.refptr._ZTV6Sphere]+0x0):undefined reference to 'vtable for Sphere' collect2: error: ld returned 1 exit status
Before pasting relevant code sections below:
Compiling in Cygwin 64 bit environment under Windows 7 Professional.
Abstract Base Class: Primitive Child Class: Sphere
Below is from file Primitive.h
#include "Color.h"
#include "Vector3D.h"
#ifndef PRIMITIVE_H
#define PRIMITIVE_H
class Primitive
{
public:
Primitive();
virtual ~Primitive();
virtual bool intersection(double &, const Vector3D &) = 0; // pure virutal function
// other functions and private variables omitted for clarity.
};
#include "Primitive.cpp"
#endif
Below is from Sphere.h
#include "Primitive.h"
#include "Point3D.h"
#include <iostream>
#include "Vector3D.h"
using namespace std;
#ifndef SPHERE_H
#define SPHERE_H
class Sphere : public Primitive
{
public:
Sphere(const Point3D &, double, const Color &, double, const Color &, double, double);
Sphere(const Point3D &, double, const Color &, double, const Color &, double, double, const Color &, double, double);
bool intersection(double &, const Vector3D &);
Point3D getCenter();
double getRadius();
~Sphere();
friend ostream & operator << (ostream &, const Sphere &);
protected:
Point3D *center;
double radius;
};
#include "Sphere.cpp"
#endif
Below is from Sphere.cpp (Parts not necessary omitted for clarity...)
#include <iostream>
#include "Point3D.h"
#include "Primitive.h"
#include "Sphere.h"
#include "Color.h"
#include "Vector3D.h"
using namespace std;
bool intersection(double &intersect, const Vector3D &ray)
{
//just trying to get it to compile.
cout << "Hello.\n";
return true;
}
I have overwritten the pure virtual function. I have even tried compiling by adding the "virtual" keyword in the declaration of Sphere.h but I get the same error.
There are NO instances of sphere objects in the code yet. When I include Sphere.h and try to compile I get the above error. When I comment out the include of Sphere.h the program compiles.
Any help is greatly appreciated. I will keep searching google to see if I come across a solution.