.:: Bots United ::.  
filebase forums discord server github wiki web
cubebot epodbot fritzbot gravebot grogbot hpbbot ivpbot jkbotti joebot
meanmod podbotmm racc rcbot realbot sandbot shrikebot soulfathermaps yapb

Go Back   .:: Bots United ::. > Developer's Farm > General Programming
General Programming Help others and get yourself helped here!

Reply
 
Thread Tools
need a 2nd pair of eyes for c++ inheritance problem
Old
  (#1)
Tactifool
Guest
 
Status:
Posts: n/a
Default need a 2nd pair of eyes for c++ inheritance problem - 22-03-2004

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;
}
  
Reply With Quote
Re: need a 2nd pair of eyes for c++ inheritance problem
Old
  (#2)
Pierre-Marie Baty
Roi de France
 
Pierre-Marie Baty's Avatar
 
Status: Offline
Posts: 5,049
Join Date: Nov 2003
Location: 46°43'60N 0°43'0W 0.187A
Default Re: need a 2nd pair of eyes for c++ inheritance problem - 22-03-2004

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.



RACC home - Bots-United: beer, babies & bots (especially the latter)
"Learn to think by yourself, else others will do it for you."
  
Reply With Quote
Re: need a 2nd pair of eyes for c++ inheritance problem
Old
  (#3)
botmeister
Ex-Council Member
 
botmeister's Avatar
 
Status: Offline
Posts: 1,090
Join Date: Nov 2003
Location: Canada
Default Re: need a 2nd pair of eyes for c++ inheritance problem - 22-03-2004

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.


Maker of the (mEAn) Bot.Admin Manager

"In theory, there is no difference between theory and practice. But, in practice, there is." - Jan L.A. van de Snepscheut
  
Reply With Quote
Re: need a 2nd pair of eyes for c++ inheritance problem
Old
  (#4)
botman
Super Moderator
 
Status: Offline
Posts: 280
Join Date: Jan 2004
Location: Plano, TX
Default Re: need a 2nd pair of eyes for c++ inheritance problem - 22-03-2004

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
  
Reply With Quote
Re: need a 2nd pair of eyes for c++ inheritance problem
Old
  (#5)
botmeister
Ex-Council Member
 
botmeister's Avatar
 
Status: Offline
Posts: 1,090
Join Date: Nov 2003
Location: Canada
Default Re: need a 2nd pair of eyes for c++ inheritance problem - 22-03-2004

You could try the Standard Template Library sort algo.

http://www.cs.rpi.edu/projects/STL/htdocs/stl.html


Maker of the (mEAn) Bot.Admin Manager

"In theory, there is no difference between theory and practice. But, in practice, there is." - Jan L.A. van de Snepscheut
  
Reply With Quote
Re: need a 2nd pair of eyes for c++ inheritance problem
Old
  (#6)
@$3.1415rin
Council Member, Author of JoeBOT
 
@$3.1415rin's Avatar
 
Status: Offline
Posts: 1,381
Join Date: Nov 2003
Location: Germany
Default Re: need a 2nd pair of eyes for c++ inheritance problem - 22-03-2004

to get from a derived class to a base class you should try dynamic_cast<...>


  
Reply With Quote
Re: need a 2nd pair of eyes for c++ inheritance problem
Old
  (#7)
Rick
Council Member
 
Rick's Avatar
 
Status: Offline
Posts: 690
Join Date: Dec 2003
Location: Holland
Default Re: need a 2nd pair of eyes for c++ inheritance problem - 22-03-2004

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
  
Reply With Quote
Re: need a 2nd pair of eyes for c++ inheritance problem
Old
  (#8)
Tactifool
Guest
 
Status:
Posts: n/a
Default Re: need a 2nd pair of eyes for c++ inheritance problem - 30-03-2004

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
}
  
Reply With Quote
Re: need a 2nd pair of eyes for c++ inheritance problem
Old
  (#9)
koraX
Member
 
koraX's Avatar
 
Status: Offline
Posts: 145
Join Date: Jan 2004
Location: Slovak Republic
Default Re: need a 2nd pair of eyes for c++ inheritance problem - 30-03-2004

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;



kXBot
koraX's utils
- see my homepage for other projects (OpenGL CSG Editor, FAT16 Sim, NNetwork Sim, ...)
  
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
vBulletin Skin developed by: vBStyles.com