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.

Attachments

When you do a new on Color() the compiler notes that the Color() object has abstract virtuals. You defined an object called Violet. So, you need to do a new on Violet(), not Color()

You can still assign it to a Color* pointer, but the new has to be on the subclass:

Color* violet = new Violet;
Color* green = new Green;
Violet* violet = new Violet; // also ok, of course

Ideally, the user decides what wavelength is output and inputs the color to make it happen. Should I not be using object handles to do this?

When I tried it originally, I got many errors (hence the

//string theColor;
//cin >> theColor;

in the main code).

I guess what I'm asking is, would the code look like:

string theColor;
cin >> theColor;
Color* theColor = new theColor;

Is this valid?

no, but I guess I don't understand the problem enough. Color has abstract virtuals, which means that you must declare a subclass of Color that has those virtuals defined, and it is the SUBCLASS that you must use.

Then you showed a Violet subclass of Color. Why can't you construct that?

Or, if you ask the customer to enter the wavelength, you could have a subclass with just a variable for the wavelength, but I don't understand what 'next' and 'prior' have to do with colors, so I'm not sure I can help here.

The problem used the Colors class heirarchy developed from the textbook's website. "next" and "previous" were included in those files as the textbook created it to print a table of colors and wavelengths. What I want to do is take the previously made files and just change the main program file. The change would allow a user to input a color and get the wavelength for it vs. a "hard-coded" table output.

In a nutshell, how are subclasses used with interactive input? I hope this makes more sense.

Instead of your 'Violet' class, then, maybe have a 'VariableColor' class with a number for the wavelength (or RGB for color or whatever), and you would construct one of THOSE and give it the input wavelength.

This article has been dead for over six months. Start a new discussion instead.