I have a gizmo class with 2 virtual methods. I can put gizmos in my program, the compiler is happy with them and their methods run happily when I call the base class's methods. Very nice.
Now, I want each gizmo to have 0 or more thingies, and each thingy has a different "DoIt" method. Being an antique Pascal programmer, I thought first of a linked list. So I added a Thingy * to my Gizmo class, and a constructor on the Thingy that builds a linked list. The Thingies come in reverse order in the list, but I don't care. If I did, it would be easy to fix.
The trouble comes when I try to add a virtual method to the Thingy class. It does not let me to that unless I derive classes for each different thingy, but once I do that, it doesn't use the base class's constructor.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
class Thingy ;
class Gizmo
{
public:
Thingy * pThingy = nullptr ;
virtual void Switch() ;
virtual int Loop(unsigned long ms)=0;
};
class Thingy
{
public:
Thingy * pNext ;
int x ; int y; int w; int h; int id ;
Thingy( int px, int py, int pw, int ph, int pid, Gizmo *pS) ;
virtual void DoIt() ;
};
void Thingy::DoIt()
{
}
class AGizmo: public Gizmo
{
public:
virtual void Switch() ;
virtual int Loop(unsigned long ms);
};
class pThing1: Thingy
{
public:
void DoIt() ;
};
pThing1 Thing1(5, 200, 60, 30, 1, &AGizmo);
pThing1::DoIt()
{
}
| |
tempscreen.cpp:26:56: error: no matching function for call to 'pThing1::Thing1(int, int, int, int, int, Gizmo*)'
pThing1 Thing1(5, 200, 60, 30, 1, &AGizmo);
But now the compiler doesn't find the constructor for pThing1. I thought it would use the constructor in Thingy to build the linked list. If I don't make the pThing1 class and just make objects of type Thingy, the constructor seems happy, but it doesn't let me make Thing1::DoIt() methods.
(I translated this by hand from what I'm really trying to do into Gizmo and Thingy because I did not want suggestions of libraries to do what I'm trying to do, I want to understand the ways of C++ better.)
Puzzled, but not astonished,
Tentoes