Problem With Struct Constructor and Abstract Class

Hi,

I need a little help spotting an error in my code. I am making a snake game just to give some context.

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//LevelObject.hpp
class LevelObject
{
    public:
        virtual void Update() = 0;
        virtual void Draw(Canvas& canvas) = 0;
    protected:
        Vector3 location_;
        Size size_;
        Collider collider_;
        Image image_;
};

//Size struct
struct Size
{
    Size(int width_, int height_) : width(width_), height(height_)
    {}
    int width, height;
};

//PlayerNode.hpp
class PlayerNode : public LevelObject
{
    public:
        PlayerNode(Vector3 loc, Size s);
        void Update();
        void Draw(Canvas& canvas);
};
//PlayerNode.cpp
PlayerNode::PlayerNode(Vector3 loc, Size s)
{
    size_ = Size(s);
    location_= Vector3(loc);
    collider_ = Collider(Rect(loc.getX(), loc.getY(), s.width, s.height));
    ImageFile("resources/images/SnakePart.bmp").load(image_);
}
void PlayerNode::Update()
{

}
void PlayerNode::Draw(Canvas& canvas)
{
    canvas.blit(image_, 0, 0, size_.width, size_.height, location_.getX(), location_.getY());
}
//Player.hpp
class Player
{
    public:
        Player(Vector3 loc, Size s);
        void Update();
        void Draw(Canvas& canvas);
    private:
        const int startLength_ = 4;
        int length_;
        std::list<PlayerNode> pNodes_;
};
//Player.hpp
Player::Player(Vector3 loc, Size s)
{
    length_ = startLength_;
    //pNodes_ = List<PlayerNode>.();
    for ( int i = 0; i < length_; i ++)
    {
        pNodes_.push_front(PlayerNode(loc + Vector3(-i,0,0), s));
        length_++;
    }
}
void Player::Update()
{

}
void Player::Draw(Canvas& canvas)
{
    for (std::list<PlayerNode>::iterator i = pNodes_.begin(); i != pNodes_.end(); i++)
    {
        i->Draw(canvas);
    }
}


The problem I have is with the Size constructor and the abstract class LevelObject which size is a member of.

The compiler error I get is:

C:\Program Files (x86)\Programming\Projects\University\prg_interactive\snakey_takey\src\..\inc\..\inc\Player.hpp|17|warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]|
C:\Program Files (x86)\Programming\Projects\University\prg_interactive\snakey_takey\src\..\inc\..\inc\..\inc\Player.hpp|17|warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]|
C:\Program Files (x86)\Programming\Projects\University\prg_interactive\snakey_takey\src\..\inc\Player.hpp|17|warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]|
C:\Program Files (x86)\Programming\Projects\University\prg_interactive\snakey_takey\src\..\inc\..\inc\LevelObject.hpp||In constructor 'LevelObject::LevelObject()':|
C:\Program Files (x86)\Programming\Projects\University\prg_interactive\snakey_takey\src\..\inc\..\inc\LevelObject.hpp|11|error: no matching function for call to 'Size::Size()'|
C:\Program Files (x86)\Programming\Projects\University\prg_interactive\snakey_takey\src\..\inc\..\inc\LevelObject.hpp|11|note: candidates are:|
C:\Program Files (x86)\Programming\Projects\University\prg_interactive\snakey_takey\src\..\inc\..\inc\..\inc\..\inc\rect.hpp|11|note: Size::Size(int, int)|
C:\Program Files (x86)\Programming\Projects\University\prg_interactive\snakey_takey\src\..\inc\..\inc\..\inc\..\inc\rect.hpp|11|note:   candidate expects 2 arguments, 0 provided|
C:\Program Files (x86)\Programming\Projects\University\prg_interactive\snakey_takey\src\..\inc\..\inc\..\inc\..\inc\rect.hpp|9|note: Size::Size(const Size&)|
C:\Program Files (x86)\Programming\Projects\University\prg_interactive\snakey_takey\src\..\inc\..\inc\..\inc\..\inc\rect.hpp|9|note:   candidate expects 1 argument, 0 provided|
C:\Program Files (x86)\Programming\Projects\University\prg_interactive\snakey_takey\src\PlayerNode.cpp||In constructor 'PlayerNode::PlayerNode(Vector3, Size)':|
C:\Program Files (x86)\Programming\Projects\University\prg_interactive\snakey_takey\src\PlayerNode.cpp|3|note: synthesized method 'LevelObject::LevelObject()' first required here |
||=== Build finished: 1 errors, 3 warnings (0 minutes, 1 seconds) ===|


However I do invoke the copy constructor when I pass a variable of type size to the constructor in this line:

size_ = Size(s);

But the problem is that its complaining that the abstract class LevelObject doesn't invoke the constructor, which it shouldn't.
Add a default constructor to the struct.
Thanks
Topic archived. No new replies allowed.