Hi,

Hoping someone can tel me where I'm going wrong, or what I should do.

I'm trying to use some pure virtual functions on some polymorphic classess but am unsure how to do it correctly.

Hiopefully this code will explain it a bit better:

vehicle.h :

using namespace std;

class Vehicle
{
public:	
	Vehicle(int publicmpg = 0); 

protected:

	int protectedmpg;  
};

Vehicle::Vehicle(int publicmpg) 
{
	protectedmpg = publicmpg; 	
}

/////////////////////////////////

class VectorClass
{
public:

	//Declaring pure virtual function here causing Vehicle to become the ABC
	virtual void AddACar() = 0; 

protected:

};

/////////////////////////////////

class SportsCar : public Vehicle
{
public:
	SportsCar(int publicsixspeed = 0, int publicmpg = 0);

	//pure virtual member function for SportsCar
	void AddACar()
	{
		cout << "Inside myfunc() for SportsCar objects" << endl;
	} 

protected:
	int protectedsixspeed;
};

SportsCar::SportsCar(int publicsixspeed, int publicmpg) : Vehicle(publicmpg) 
{
	protectedsixspeed = publicsixspeed;
}

main.cpp :

#include <iostream>
#include <vector>
#include "Vehicle.h"

using namespace std;

int main()
{
	vector<Vehicle*> vectorname;

	int publicmpg = 11;
	int publicsixspeed = 22;

	SportsCar node(publicmpg, publicsixspeed);

	SportsCar *ptrToSportsCar;

	ptrToSportsCar = &node;

	vectorname.push_back( ptrToSportsCar );

	cout << "past push" << endl;


	vectorname[0]->AddACar(); 
	

	cout << "Past myfunc() call " << endl;

	system ("PAUSE");
	return 0;
}

Visual Studio reports:

'DeleteACar' : is not a member of 'Vehicle'

I'm going to use DeleteACar() to delete nodes from the Vector by passing which node as an argument, but before I attempt to write the function, I'm stuck as how to correctly configure a pure virtual function.

Any advice or advice is most appreciated.

Thanks!

Carrots.

Recommended Answers

All 7 Replies

in line 33 you should inherit from VectorClass to use the virtual method AddACar().
i just wanted to warn cuz the method AddACar() u used in main
isn't the virtual function that u declared in VectorClass.

Secondly i dont see any member function or attributes u declared named DeleteACar from where did the compiler get it??? plz tell the line that the error occured in.

Thanks very much Ahmed !!

Where am I going wrong here?

I've tried:

class SportsCar : public VectorClass

class SportsCar : public Vehicle, public VectorClass

and

class SportsCar : public VectorClass, public Vehicle

None seem to work, where am I going wrong?


EDIT - Thanks Ahmed, just seen your edit and just getting my head around it. I haven't added any functionality to the function apart from the cout as I wanted to get a simplem function working before creating it fully.

please could u tell me more about your problem do u want to solve the compiler's error or do u want to know how to configure a pure virtual function?

Solve the compiler's error for sure, that would be great. :)

Thanks very much, your taking the time to help me out is most apreciated 100%!

ok the compiler's error is

DeleteACar' : is not a member of 'Vehicle'

and in your code i dont see any data or function named DeleteACar
so could u please tell check your code for DeleteACar.

But i think that u declared a vector with type Vehicle* and u pushed a pointer with type Sportscar .
when u say

vectorname[0]->AddACar();

it means that u call AddACar() of the vehicle class which vehicle class hasnt this method .

So the solution is let the vehicle class be derived from VectorClass and implement AddACar() method for the vehicle class.
Your problem is in Polymorphism .
I hope that this is the solution for your problem.

AddAcar should be a method of the vector, and probably deleteacar too judging by their names. SportsCar should inherit only from vehicle. The virtual function should be declared in vehicle (don't need =0; after function declaration :) ) and then overriden in sportscar.

Noticing the 'solved threads' username tag, I can now say this thread is fully solved aswell! :)

Thank you so much, the classes are now fully working.

This is a great forum.

Here are the working classes now.

Using the VectorClass as the Interface makes everything flow down through the inherited classes nicely.

using namespace std;


class VectorClass
{
public:

	//Declaring pure virtual function here causing VectorClass to become an A.B.C.
	virtual void DeleteACar() = 0; 

protected:

};

/////////////////////////////////


class Vehicle : public VectorClass
{
public:	
	Vehicle(int publicmpg = 0); 

protected:

	int protectedmpg;  
};

Vehicle::Vehicle(int publicmpg) 
{
	protectedmpg = publicmpg; 	
}

/////////////////////////////////



/////////////////////////////////

//class SportsCar : public Vehicle
class SportsCar : public Vehicle
{
public:
	SportsCar(int publicsixspeed = 0, int publicmpg = 0);

	//pure virtual member function for SportsCar
	void DeleteACar()
	{
		cout << "Inside DeleteACar() for SportsCar objects" << endl;
	} 

protected:
	int protectedsixspeed;
};

SportsCar::SportsCar(int publicsixspeed, int publicmpg) : Vehicle(publicmpg) 
{
	protectedsixspeed = publicsixspeed;
}


//create the virtual function

//void DeleteACar(pointer to car or location within vector[])
void DeleteACar()
{

//	int x = vectorsize();

	cout << "Vector size is : "<< size() << endl;
	
	//for(int a = 0;a< vector size();a++)

	//{
	//	if vectorname[a] = match

	//	{
	//		delete vectorname[a];  
	//		break;
	//	}
	//}

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.