| | |
Pure Virtual Functions. beginner help sought.
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2009
Posts: 31
Reputation:
Solved Threads: 0
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 :
main.cpp :
Visual Studio reports:
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.
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 :
C++ Syntax (Toggle Plain Text)
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 :
C++ Syntax (Toggle Plain Text)
#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'
Any advice or advice is most appreciated.
Thanks!
Carrots.
Last edited by Carrots; May 30th, 2009 at 6:38 pm.
•
•
Join Date: Nov 2008
Posts: 32
Reputation:
Solved Threads: 1
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.
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.
Last edited by Ahmed_I; May 30th, 2009 at 7:09 pm.
•
•
Join Date: Mar 2009
Posts: 31
Reputation:
Solved Threads: 0
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.
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.
Last edited by Carrots; May 30th, 2009 at 7:20 pm.
•
•
Join Date: Nov 2008
Posts: 32
Reputation:
Solved Threads: 1
ok the compiler's error is
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
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.
C++ Syntax (Toggle Plain Text)
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();
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.
Last edited by Ahmed_I; May 30th, 2009 at 8:05 pm.
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.
) and then overriden in sportscar. 92% of all statistics are made up on the spot.
If you found a post helpful, please click the "give XXX reputation" link, to show your appreciation to those who helped you. Thanks! :D
If you found a post helpful, please click the "give XXX reputation" link, to show your appreciation to those who helped you. Thanks! :D
•
•
Join Date: Mar 2009
Posts: 31
Reputation:
Solved Threads: 0
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.

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.
c++ Syntax (Toggle Plain Text)
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; // } //} }
![]() |
Similar Threads
- Q. virtual functions (C++)
- Runtime Error - R 6025 - pure virtual function call (Windows NT / 2000 / XP)
- Virtual functions (C++)
- Virtual functions, function objects, or what? (C)
- C++ derived class virtual functions (C++)
- Dealing w/ virtual functions (C++)
Other Threads in the C++ Forum
- Previous Thread: c++ error code?
- Next Thread: Date Function not returning variable in main
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





