Not really understanding classes and inheritance.

Apr 5, 2019 at 6:08pm
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
Last edited on Apr 5, 2019 at 6:13pm
Apr 5, 2019 at 7:06pm
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.


This being C++, a vector is the way to do that, rather than a linked list. This being C++, you need a really good reason to use a linked list. That's just by-the-by, though.


pThing1 Thing1(5, 200, 60, 30, 1, &AGizmo);
This is an attempt to create an object of type pThing1 using, as you suspect, a constructor that does not exist. In C++ you need to specifically state that you want to inherit constructors.

class pThing1: Thingy
{
using Thingy::Thingy;
public:
  void DoIt() ;
}; 



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,

I don't follow. How can you have different thingy objects without deriving classes for them?
Last edited on Apr 5, 2019 at 7:09pm
Apr 5, 2019 at 9:49pm
Oh. I see: using Thingy::Thingy; is what I'm missing.

I'll read up on vectors.

Thanks,

Tentoes
Apr 5, 2019 at 10:01pm
"using" seems to have done the trick. I'll spend some time reading this "vector" article.
Apr 5, 2019 at 10:52pm
After your code editing, your first post is no more connected with your reported issues.
Tentoes wrote:
tempscreen.cpp:26:56: error: no matching function for call to 'pThing1::Thing1(int, int, int, int, int, Gizmo*)'

Now your line 26 is:
{

As your code is now, I think you’d better take care of what the compiler points out before trying to guess further issues:
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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() ;
}; 


// This is a standalone function prototype, i.e. the prototype of a function
// that does not belong to any class.
// Being a prototype, you need to specify the types for your parameters.
// int is assumed -----||
//                     \/         \/----- Reference to...?
pThing1 Thing1(5, 200, 60, 30, 1, &AGizmo);
// error: expected primary-expression before ')' token
// pThing1 Thing1(5, 200, 60, 30, 1, &AGizmo);
//                                          ^


// Missing return type specification: int is assumed; but there's just one
// prototype for this function, and that prototype declares the function to be
// of type void:
pThing1::DoIt()
// error: no declaration matches 'int pThing1::DoIt()'
// pThing1::DoIt()
// ^~~~~~~
{
}

// Just to compile:
int main() {}


When you post your code, please add a main() - that makes it faster to check your code on an online compiler by a simple click.
Topic archived. No new replies allowed.