I've been fighting w/ this problem for about 3 weeks. My school's forum and another programming forum I belong to have been unable to help me (I can't get ahold of my instructor but the school is looking into that).
When running the attached program, I get the errors
/home/cody/Projects/wavelength/src/wavelength.cpp:19: cannot allocate an object of type `Color'
*/home/cody/Projects/wavelength/src/wavelength.cpp:19: because the following virtual functions are abstract:
*/home/cody/Projects/wavelength/src/colors.h:26: virtual Color* Color:: previous() const
*/home/cody/Projects/wavelength/src/colors.h:25: virtual Color* Color::next() const
*/home/cody/Projects/wavelength/src/colors.h:23: virtual double Color::wavelength() const
(For ease, the main file is below)
#include <iostream>
#include "colors.h"
using namespace std;
int main()
{
cout << "To compute the wavelength corresponding to a given color,\n"
<< "enter a color (e.g. Red): ";
//string theColor;
//cin >> theColor;
Color* colorHandle = new Color();
cout << "\nThe corresponding wavelength is " << colorHandle->wavelength() << endl;
return 0;
}
I vaguely understand why this is happening; because the Color class and subclasses are abstract, the subclasses must provide a definition of the virtual method. But that's what I thought they did, for example:
// ***** Violet operations *************************************
inline Violet::Violet() : Color("Violet")
{}
inline double Violet::wavelength() const
{
return 4.1e-7;
}
inline Color* Violet::next() const
{
return NULL;
}
inline Color* Violet::previous() const
{
return new Indigo();
}
Now, the colors.h and colors.cpp files were taken from the textbook website, as the actual problem is to use the Colors class hierarchy to create a program that gives the wavelength of light for a given color. The 2 files were originally for a program that used handles to output a table of color properties.
If I change the Color* to
Color* colorHandle = new theColor();
then all I get is a "syntax error before the ';' token".
Even if I don't use handles, I still get "virtual function" errors if I try to create a Color instance. Is there a correct way to input a color using the Color hierarchy? I've looked through the website's code and corrected what I think were deliberate errors but may have missed some.