GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 16-Nov-2004, 22:34
crystalattice's Avatar
crystalattice crystalattice is offline
Flame War Instigator
 
Join Date: Apr 2004
Location: San Diego
Posts: 1,534
crystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nice

Dealing with virtual functions


When running the attached program, I get the errors
Code:
/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)
CPP / C++ / C Code:
#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:
CPP / C++ / C Code:
// ***** 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
CPP / C++ / C Code:
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.
Attached Files
File Type: zip src.zip (2.5 KB, 32 views)
__________________
Common Sense v2.0-Striving to make the world a little bit smarter.
 

Recent GIDBlogNARMY by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Please help with functions... brookeville CPP / C++ Forum 36 05-Nov-2004 00:23
conflict between printf and stdarg.h va functions mirizar C Programming Language 3 12-Jul-2004 08:11
Another problem dealing with main() and driver function tommy69 C Programming Language 4 20-Mar-2004 19:46
Understanding functions tommy69 C Programming Language 15 15-Mar-2004 17:59

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 16:29.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.