![]() |
need a 2nd pair of eyes for c++ inheritance problem
Im workin on a sorting algorithm and I want it to work for any class that inherits from class Sortable. Im using microsoft studio .NET 2003 to compile it. I stripped out all the actual sorting code and left in what was giving me the error. It says that class CharCount cannot be converted into class Sortable and Im wondering why this is the case. Im pretty much stumped as to what the problem is. Its probably something pretty obvious and Id appreciate any help anyone can offer.
here is my header file(main.h): class Sortable { public: Sortable() {}; virtual ~Sortable() {}; public: virtual int Value() = 0; }; class CharCount : public Sortable { public: CharCount(){character = 0;count = 0;} virtual ~CharCount() {}; void Init(char theChar) {character = theChar;} public: char Character() {return character;} int Value() {return count;} void Increment() {count++;} private: int count; char character; }; this is the c file (main.cpp): #include "main.h" void Sort(Sortable **table, int size) { } int main() { CharCount **table = new CharCount *[20]; for(int i = 0; i < 20; i++) { table[i] = new CharCount(); } Sort(table, 20); // the compiler finds the error here //(cannot convert from CharCount** to Sortable**) return 0; } |
Re: need a 2nd pair of eyes for c++ inheritance problem
IMO this is not an inheritance problem because your classes are inheritated fine from each other, but I think the problem comes that you can't assimilate a child class to its parent when trying to pass an instance of it as a parameter to the Sort() function. You'd better write overloaded versions of Sort() that would feature the parameter types you want and let the compiler link against the right one.
Other than that I don't know what a "Sort ((Sortable **) table, 20)" would give... I'm not a big C++ guy anyway. |
Re: need a 2nd pair of eyes for c++ inheritance problem
I think PMB is correct with his assesment. Here's another way of looking at the problem.
There's really nothing mysterious about the error. You are simply trying to pass a CharCount type to the function Sort which is expecting Sortable as the type for table, and this is what is causing the type mismatch. It's as if you are trying to pass an int when a float was expected. I think you probably understand the basics of data typing, so you clearly have a big misunderstanding with how inheritence works. If you can tell us what you thought the compiler would do with your code, then perhaps we'll see where your ideas of inheritance are incorrect. |
Re: need a 2nd pair of eyes for c++ inheritance problem
What you are trying to do you need to use C++ Templates for.
Templates allow you to create functions that take any arbitrary data type and operate on them. For example you could have a sort function that takes ints, floats, chars, character string pointers, automobile classes, fishing hook classes, pointers to structures, etc. See the cplusplus.com tutorial on templates... http://www.cplusplus.com/doc/tutorial/tut5-1.html botman |
Re: need a 2nd pair of eyes for c++ inheritance problem
You could try the Standard Template Library sort algo.
http://www.cs.rpi.edu/projects/STL/htdocs/stl.html |
Re: need a 2nd pair of eyes for c++ inheritance problem
to get from a derived class to a base class you should try dynamic_cast<...>
|
Re: need a 2nd pair of eyes for c++ inheritance problem
I tried dynamic_cast on his code, doesn't work for some reason :| It doesn't seem to work with pointers of pointers i guess
|
Re: need a 2nd pair of eyes for c++ inheritance problem
Sorry for takin so long to repost. The problem is that you cannot convert a type that is a pointers to pointers to another pointer to pointer type (CharCount ** cannot be converted to a Sortable**). It turs out its simply a rule of the language. I did find a solution by using templates. This is how it ended up looking.
template <typename T> inline void QuickSort(T **objects, int size) { // QSort(objects, 0, size - 1); // the recursive quick sort funtcion } |
Re: need a 2nd pair of eyes for c++ inheritance problem
Quote:
I once did this. PHP Code:
|
All times are GMT +2. The time now is 17:07. |
Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.