943,675 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 565
  • C++ RSS
May 30th, 2009
0

Pure Virtual Functions. beginner help sought.

Expand Post »
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 :
C++ Syntax (Toggle Plain Text)
  1. using namespace std;
  2.  
  3. class Vehicle
  4. {
  5. public:
  6. Vehicle(int publicmpg = 0);
  7.  
  8. protected:
  9.  
  10. int protectedmpg;
  11. };
  12.  
  13. Vehicle::Vehicle(int publicmpg)
  14. {
  15. protectedmpg = publicmpg;
  16. }
  17.  
  18. /////////////////////////////////
  19.  
  20. class VectorClass
  21. {
  22. public:
  23.  
  24. //Declaring pure virtual function here causing Vehicle to become the ABC
  25. virtual void AddACar() = 0;
  26.  
  27. protected:
  28.  
  29. };
  30.  
  31. /////////////////////////////////
  32.  
  33. class SportsCar : public Vehicle
  34. {
  35. public:
  36. SportsCar(int publicsixspeed = 0, int publicmpg = 0);
  37.  
  38. //pure virtual member function for SportsCar
  39. void AddACar()
  40. {
  41. cout << "Inside myfunc() for SportsCar objects" << endl;
  42. }
  43.  
  44. protected:
  45. int protectedsixspeed;
  46. };
  47.  
  48. SportsCar::SportsCar(int publicsixspeed, int publicmpg) : Vehicle(publicmpg)
  49. {
  50. protectedsixspeed = publicsixspeed;
  51. }



main.cpp :
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <vector>
  3. #include "Vehicle.h"
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. vector<Vehicle*> vectorname;
  10.  
  11. int publicmpg = 11;
  12. int publicsixspeed = 22;
  13.  
  14. SportsCar node(publicmpg, publicsixspeed);
  15.  
  16. SportsCar *ptrToSportsCar;
  17.  
  18. ptrToSportsCar = &node;
  19.  
  20. vectorname.push_back( ptrToSportsCar );
  21.  
  22. cout << "past push" << endl;
  23.  
  24.  
  25. vectorname[0]->AddACar();
  26.  
  27.  
  28. cout << "Past myfunc() call " << endl;
  29.  
  30. system ("PAUSE");
  31. return 0;
  32. }

Visual Studio reports:

Quote ...
'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.
Last edited by Carrots; May 30th, 2009 at 6:38 pm.
Similar Threads
Reputation Points: 41
Solved Threads: 0
Junior Poster in Training
Carrots is offline Offline
62 posts
since Mar 2009
May 30th, 2009
0

Re: Pure Virtual Functions. beginner help sought.

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.
Last edited by Ahmed_I; May 30th, 2009 at 7:09 pm.
Reputation Points: 14
Solved Threads: 1
Light Poster
Ahmed_I is offline Offline
32 posts
since Nov 2008
May 30th, 2009
0

Re: Pure Virtual Functions. beginner help sought.

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.
Last edited by Carrots; May 30th, 2009 at 7:20 pm.
Reputation Points: 41
Solved Threads: 0
Junior Poster in Training
Carrots is offline Offline
62 posts
since Mar 2009
May 30th, 2009
0

Re: Pure Virtual Functions. beginner help sought.

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?
Reputation Points: 14
Solved Threads: 1
Light Poster
Ahmed_I is offline Offline
32 posts
since Nov 2008
May 30th, 2009
0

Re: Pure Virtual Functions. beginner help sought.

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%!
Reputation Points: 41
Solved Threads: 0
Junior Poster in Training
Carrots is offline Offline
62 posts
since Mar 2009
May 30th, 2009
0

Re: Pure Virtual Functions. beginner help sought.

ok the compiler's error is
C++ Syntax (Toggle Plain Text)
  1. DeleteACar' : is not a member of 'Vehicle'
  2.  

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
Quote ...
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.
Last edited by Ahmed_I; May 30th, 2009 at 8:05 pm.
Reputation Points: 14
Solved Threads: 1
Light Poster
Ahmed_I is offline Offline
32 posts
since Nov 2008
May 30th, 2009
0

Re: Pure Virtual Functions. beginner help sought.

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.
Reputation Points: 14
Solved Threads: 22
Junior Poster
JugglerDrummer is offline Offline
138 posts
since Apr 2009
May 30th, 2009
0

Re: Pure Virtual Functions. beginner help sought.

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.


c++ Syntax (Toggle Plain Text)
  1. using namespace std;
  2.  
  3.  
  4. class VectorClass
  5. {
  6. public:
  7.  
  8. //Declaring pure virtual function here causing VectorClass to become an A.B.C.
  9. virtual void DeleteACar() = 0;
  10.  
  11. protected:
  12.  
  13. };
  14.  
  15. /////////////////////////////////
  16.  
  17.  
  18. class Vehicle : public VectorClass
  19. {
  20. public:
  21. Vehicle(int publicmpg = 0);
  22.  
  23. protected:
  24.  
  25. int protectedmpg;
  26. };
  27.  
  28. Vehicle::Vehicle(int publicmpg)
  29. {
  30. protectedmpg = publicmpg;
  31. }
  32.  
  33. /////////////////////////////////
  34.  
  35.  
  36.  
  37. /////////////////////////////////
  38.  
  39. //class SportsCar : public Vehicle
  40. class SportsCar : public Vehicle
  41. {
  42. public:
  43. SportsCar(int publicsixspeed = 0, int publicmpg = 0);
  44.  
  45. //pure virtual member function for SportsCar
  46. void DeleteACar()
  47. {
  48. cout << "Inside DeleteACar() for SportsCar objects" << endl;
  49. }
  50.  
  51. protected:
  52. int protectedsixspeed;
  53. };
  54.  
  55. SportsCar::SportsCar(int publicsixspeed, int publicmpg) : Vehicle(publicmpg)
  56. {
  57. protectedsixspeed = publicsixspeed;
  58. }
  59.  
  60.  
  61. //create the virtual function
  62.  
  63. //void DeleteACar(pointer to car or location within vector[])
  64. void DeleteACar()
  65. {
  66.  
  67. // int x = vectorsize();
  68.  
  69. cout << "Vector size is : "<< size() << endl;
  70.  
  71. //for(int a = 0;a< vector size();a++)
  72.  
  73. //{
  74. // if vectorname[a] = match
  75.  
  76. // {
  77. // delete vectorname[a];
  78. // break;
  79. // }
  80. //}
  81.  
  82. }
Reputation Points: 41
Solved Threads: 0
Junior Poster in Training
Carrots is offline Offline
62 posts
since Mar 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: c++ error code?
Next Thread in C++ Forum Timeline: Date Function not returning variable in main





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC