C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-30-2002, 05:10 PM   #1
Registered User
 
Join Date: Jun 2002
Posts: 59
Abstract class problem

Hello,

I have an abstract class entitled "Layer" and a class derived from it entitled "Point".

I understand one cannot make an object of type "Layer" but I want to make an object of type "Point".

When I try to create an object of type "Point" like the following:
Code:
Point * PointPtr = new Point("univ", fileStruct.name, wsn, curDataDirArg);
I get the following error msg:
Code:
c:\temp\c++\pec\pec2\digObj.cpp(263): error C2259: 'Point' : cannot instantiate abstract class
Here is my header file for the "Layer" class:
Code:
#ifndef LAYER_H 	//conditional compilation if the layer class is not defined
#define LAYER_H		//define the layer class

#include <iostream.h>	//for cout
#include <string.h>		//for string class
#include <ctype.h>		//for tolower()
#include <conio.h>		//for getch()

//begin the Layer class
class Layer
{
	//declare private data members
	protected:	

		//data members 
	
		//attributes for AXL
		char * layerType;		//featureclass or image
		char * dataSetType;		//point, line, polygon or image
		char * layerName;		//name for map legend layer on map
		char visible[6];		//true or false
		static int id;			//id from 0 up for all layer obj inst
		char * dataSetName;		//the dataset name (faunivs_0)
		char * workSpaceName;	//the dataset dir (shp-xxx-04) (munic)
		char * workSpaceDir;	//the dataset dir (C:\faunivs.shp)
		
		//set functions		
		virtual void setLayerType(char *) = 0;		//featureclass or image
		virtual void setDataSetType(char *) = 0;	//point, line, polygon or image
		virtual void setLayerName(char *) = 0;		//name for map legend layer on map
		virtual void setVisible(char *) = 0;		//true or false
		void setId();								//id from 0 up for all layer obj inst
		virtual void setDataSetName(char *) = 0;	//the dataset name (faunivs_0)
		virtual void setWorkSpaceName(char *) = 0;	//the dataset workspace (munic)
		void setWorkSpaceDir(char *);				//the dataset workspace (C:\pec\)
				
	//declare the public data members
	public:

		//constructor, destructor
		Layer(char *);		//constructor
		~Layer();			//destructor

		//get functions
		const char * getLayerType();		//get the layerType (featureclass or image)
		const char * getDataSetType();		//get dataset type (point, line, etc)
		const char * getLayerName();		//get layer name for map legend
		const char * getVisible();			//get visibility (true or false)
		const int getId();					//get layer id (id="1")
		const char * getDataSetName();		//get the dataset Name (fawater_0)
		const char * getWorkSpaceName();	//get the path of the dataset (shp-xxx-04)
		const char * getWorkSpaceDir();		//get the path of the dataset (C:\faunivs.shp)

		//print 
		virtual void printLayer() = 0;		//prints the layer

		       
};	//end the Layer class	

#endif LAYER_H		//conditional compilation end if the layer class is not define
And here is my header for "Point":
Code:
#ifndef POINT_H 	//conditional compilation if the county class is not defined
#define POINT_H		//define the point class

#include "Layer.h"	//include the layer base class

//begin the point class
class Point: public Layer
{
	//declare private data members
	private:

		char * color;			//set the points color
		int width;				//width of point
		
	//declare the public data members
	public:

		//constructor
		Point(char *, char *, char *, char *);		//constructor
		
		//get 
		const char * getColor();	//gets colors in one string
        const int getWidth();		//gets width

		//set
		void setColor(char *);		//sets colors to point name
		void setWidth(int);			//sets the width of the point

		//virtual functions with definitions in point class
		virtual void setLayerType();			//featureclass or image
		virtual void setLayerName(char *);		//name for map legend layer on map						
		virtual void setVisible();				//true or false
		virtual void setDataSetName(char *);	//the dataset name (faunivs_0)
		virtual void setDataSetType();			//point, line, polygon or image
		virtual void setWorkSpaceName(char *);	//the dataset workspace name (munic)

		void printLayer();
		        
};	//end the Point class	

#endif POINT_H		//conditional compilation end if the point class is not defined
As you can see I have member functions in "Point" fulfilling function definitions from "Layer".

Why won't my compiler (VS .net) let me create a "Point" object?
__________________
" . . . and I lay awake, big dreamers never sleep." - David Lee Roth
VanJay011379 is offline   Reply With Quote
Old 07-30-2002, 05:20 PM   #2
Registered User
 
Join Date: Oct 2001
Posts: 68
I don't know if i am right or not, But

Hi U,

I think your problem is that you are inherting the Layer class, but what you might be doing wrong is that you are calling the base constructor when createing a new object. when you do inhertence you adopt the member of the base else well. so these is what u can do. But i am not sure my self please double check.

Code:
 

Point(char *, char *, char *, char *):base(char * ,char * ,char *, char *,int,char *,char *,char *);
Hope these help. If i am wrong then let me know how to do it....
Thanks
__________________
simly01 is offline   Reply With Quote
Old 07-30-2002, 07:23 PM   #3
Registered User
 
Join Date: Apr 2002
Posts: 362
This is going to get ugly...(sort of)

As soon as you declare a "pure" virtual function, the class becomes abstract. You cannot inherit data members of an abstract class, only the "interface" (the member functions).

(Properly designed, (no offense), an abstract class would have no data members. Yours does.)

Backtrack and make your "pure" virtual functions "simple". That is, delete the "= 0" from your function declarations.)

You'll still be compelled to declare/define the virtual functions (from the base class) in your derived class, but the data members in the base class should still be accessible as they are "protected", i.e. accessible to "derived" classes.

As an aside, I don't see that "Point" has any function "definitions". You've re-declared the functions, but there are no "definitions". I see no code blocks indicating that you're doing anything with the arguments passed to the functions.

"printLayer()" is an example. From the code you've offered, it does nothing. It's declared (base class)/re-declared (derived class) and nothing else.

I'll defer readily, (more than readily, truth be told), but...

(Trust me. If anyone else has better information, please adhere to it.)

-Skipper
__________________
"When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow
skipper is offline   Reply With Quote
Old 07-30-2002, 07:30 PM   #4
Registered User
 
Join Date: Feb 2002
Posts: 20
Hey,

I often get those useless 'instantiate' errors, which do not help in finding the problem.

One solution might be to define in your Layer class a default constructor, which would be automatically called whenever your Point class is instantiated.

Also did you try checking your documentation for more information on 'error C2259'.
__________________
Compilers: MSVC++ 6.0, Dev-C++ 4.0, DJGPP
Operating System: Windows 98
White Rider is offline   Reply With Quote
Old 07-30-2002, 07:39 PM   #5
geek
 
SilentStrike's Avatar
 
Join Date: Aug 2001
Location: NJ
Posts: 1,141
You do inherit the data members from an abstract class, and if they are non-private, they are accessible from the derived class. Still, I agree it is a bad idea to have data in an abstract class. The only reason Point would be abstract is if it did not implement all the pure virtuals in the base class (in some other implementation file), which it looked like you did.
__________________
Prove you can code in C++ or C# at TopCoder, referrer rrenaud
Read my livejournal
SilentStrike is offline   Reply With Quote
Old 07-31-2002, 07:47 AM   #6
Registered User
 
Join Date: Jun 2002
Posts: 59
Here is what the help documentation gives:

'class' : cannot instantiate abstract class due to following members:

Code declares an instance of an abstract class or structure. One or more C4259 warnings follow for the abstract members.

You cannot instantiate a class or structure with one or more pure virtual functions. To instantiate objects of a derived class, the derived class must override each pure virtual function.

Code:
Example

// C2259.cpp
class V
{
public:
   void virtual func() = 0;
};
class A : public V {};
class B : public V
{
public:
   void func();
};
V v;  // C2259, V is an abstract class
A a;  // C2259, A inherits func() as pure virtual
B b;  // OK, B defines func()
Isn't this what I am doing?
__________________
" . . . and I lay awake, big dreamers never sleep." - David Lee Roth
VanJay011379 is offline   Reply With Quote
Old 07-31-2002, 07:57 AM   #7
Registered User
 
Join Date: Jun 2002
Posts: 59
Ok, I just figured it out. A foolish error on my behalf while modifying the classes.

If you look at the virtual function definitions in the "Layer" class and the virtual function implementations in the "Point" class, you'll see they have different arguments. I adjusted them to be consistent.

Thanks for the help and critique good people. You've helped me out a lot on this project.
__________________
" . . . and I lay awake, big dreamers never sleep." - David Lee Roth
VanJay011379 is offline   Reply With Quote
Old 07-31-2002, 08:01 AM   #8
Registered User
 
Join Date: Jun 2002
Posts: 59
Quote:
Originally posted by SilentStrike
You do inherit the data members from an abstract class, and if they are non-private, they are accessible from the derived class. Still, I agree it is a bad idea to have data in an abstract class. The only reason Point would be abstract is if it did not implement all the pure virtuals in the base class (in some other implementation file), which it looked like you did.
Hmmm. . .

Most literature I've read on Java and C++ seems very in favor of using data members in abstract classes.

In my situation, the abstract class will contain several attributes, which will be inherited by several classes.
__________________
" . . . and I lay awake, big dreamers never sleep." - David Lee Roth
VanJay011379 is offline   Reply With Quote
Old 07-31-2002, 09:41 AM   #9
Registered User
 
Join Date: Apr 2002
Posts: 362
Thank you both, SilentStrike and VanJay, for the correction on the inheritance of data members from an abstract class. Brain flatulence on my part.

-Skipper
__________________
"When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow
skipper is offline   Reply With Quote
Old 07-31-2002, 01:30 PM   #10
geek
 
SilentStrike's Avatar
 
Join Date: Aug 2001
Location: NJ
Posts: 1,141
But then your abstract class should inherit from an interface class, that has nothing but virtuals. Client code should only use the interface, so your other full implementations don't get stuck relying on a partial implementation. It makes the software easier to modify in case of change.

Code:
             Interface class (no data at all)
              /                          \
     Partial implementation            Room for concrete classes without partial implementation  in future
      /               \
Concrete class A    Concrete class B
__________________
Prove you can code in C++ or C# at TopCoder, referrer rrenaud
Read my livejournal
SilentStrike is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting an error with OpenGL: collect2: ld returned 1 exit status Lorgon Jortle C++ Programming 6 05-08-2009 08:18 PM
Issue with pointers to an abstract class and inheritance bainevii C++ Programming 2 03-31-2009 07:51 AM
abstract class xddxogm3 C++ Programming 5 01-01-2005 09:08 AM
static class problem. Sebastiani C++ Programming 3 10-16-2002 03:27 PM
Returning an object from a method - Problem when creating my own string class pecymanski C++ Programming 3 12-03-2001 01:45 PM


All times are GMT -6. The time now is 07:09 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22