.:: Bots United ::.

.:: Bots United ::. (http://forums.bots-united.com/index.php)
-   General Programming (http://forums.bots-united.com/forumdisplay.php?f=25)
-   -   need a 2nd pair of eyes for c++ inheritance problem (http://forums.bots-united.com/showthread.php?t=1152)

Tactifool 22-03-2004 01:28

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;
}

Pierre-Marie Baty 22-03-2004 04:56

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.

botmeister 22-03-2004 07:07

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.

botman 22-03-2004 14:52

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

botmeister 22-03-2004 20:29

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

@$3.1415rin 22-03-2004 20:40

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<...>

Rick 22-03-2004 21:53

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

Tactifool 30-03-2004 00:54

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
}

koraX 30-03-2004 09:18

Re: need a 2nd pair of eyes for c++ inheritance problem
 
Quote:

Originally Posted by Tactifool
Sorry for takin so long to repost. [font=verdana, arial, helvetica][size=2]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**).

I think in C++ you can convert anything to anything. Or you can use void* and convert it to anything.

I once did this.

PHP Code:

void    *test_in;
...
// dynamic array creation
test_in=(void *)new float*[numofsamples];
for(
DWORD i=0;i<numofsamples;i++)
    *((
float **)test_in+i)=new float[in_size];
...
// dynamic array destruction
if(test_in!=NULL) {
    for(
int i=0;i<test_count;i++)
        if(((
float **)test_in+i)!=NULL)    
            
delete *((float **)test_in+i);
    
delete test_in;




All times are GMT +2. The time now is 17:07.

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.