Rect Constructor

Hi I am making a "Snake" game and I am creating a collider class which has a rect class which is used to work out collisions. The problem I am having is with the rects constructor. I have a Level class with a Rect object, in the levels constructor I call the Rects constructor. However I get a compiler error saying:

C:\Program Files (x86)\Programming\Projects\University\prg_interactive\snakey_takey\src\level.cpp||In constructor 'Level::Level()':|
C:\Program Files (x86)\Programming\Projects\University\prg_interactive\snakey_takey\src\level.cpp|3|error: no matching function for call to 'Rect::Rect()'|
C:\Program Files (x86)\Programming\Projects\University\prg_interactive\snakey_takey\src\level.cpp|3|note: candidates are:|
C:\Program Files (x86)\Programming\Projects\University\prg_interactive\snakey_takey\src\..\inc\..\inc\rect.hpp|17|note: Rect::Rect(int, int, int, int)|
C:\Program Files (x86)\Programming\Projects\University\prg_interactive\snakey_takey\src\..\inc\..\inc\rect.hpp|17|note:   candidate expects 4 arguments, 0 provided|
C:\Program Files (x86)\Programming\Projects\University\prg_interactive\snakey_takey\src\..\inc\..\inc\rect.hpp|14|note: Rect::Rect(const Rect&)|
C:\Program Files (x86)\Programming\Projects\University\prg_interactive\snakey_takey\src\..\inc\..\inc\rect.hpp|14|note:   candidate expects 1 argument, 0 provided|
||=== Build finished: 1 errors, 0 warnings (0 minutes, 0 seconds) ===|


However I definatly do pass in four interger arguments.

1
2
3
4
Level::Level()
{
    square = Rect(100,100,100,100);
}


Perhaps more annoying is if I remove the four integers from my rect constructor and make it require no arguments I get the following compiler error:

C:\Program Files (x86)\Programming\Projects\University\prg_interactive\snakey_takey\src\level.cpp||In constructor 'Level::Level()':|
C:\Program Files (x86)\Programming\Projects\University\prg_interactive\snakey_takey\src\level.cpp|5|error: no matching function for call to 'Rect::Rect(int, int, int, int)'|
C:\Program Files (x86)\Programming\Projects\University\prg_interactive\snakey_takey\src\level.cpp|5|note: candidates are:|
C:\Program Files (x86)\Programming\Projects\University\prg_interactive\snakey_takey\src\..\inc\..\inc\rect.hpp|17|note: Rect::Rect()|
C:\Program Files (x86)\Programming\Projects\University\prg_interactive\snakey_takey\src\..\inc\..\inc\rect.hpp|17|note:   candidate expects 0 arguments, 4 provided|
C:\Program Files (x86)\Programming\Projects\University\prg_interactive\snakey_takey\src\..\inc\..\inc\rect.hpp|14|note: Rect::Rect(const Rect&)|
C:\Program Files (x86)\Programming\Projects\University\prg_interactive\snakey_takey\src\..\inc\..\inc\rect.hpp|14|note:   candidate expects 1 argument, 4 provided|
||=== Build finished: 1 errors, 0 warnings (0 minutes, 0 seconds) ===|


Here is the whole code for both:

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
80
81
82
83
84
85
86
87
88
89
//Level.hpp
#ifndef LEVEL_HPP
#define LEVEL_HPP

#include <prg_interactive.hpp>
#include "../inc/rect.hpp"

using namespace prg;

class Level
{
public:
    Level();
    void Update();
    void DrawLevel(Canvas& canvas);
private:
    Rect square;
};
#endif // LEVEL_HPP

//level.cpp
#include "../inc/level.hpp"

Level::Level()
{
    square = Rect(100,100,100,100);
}
void Level::Update()
{

}
void Level::DrawLevel(Canvas& canvas)
{
    square.Draw(canvas);
}
//Rect.hpp
#ifndef RECT_HPP
#define RECT_HPP

#include "../inc/Vector3.hpp"
#include <prg_interactive.hpp>

using namespace prg;

struct Size
{
    int width, height;
};

class Rect
{
    public:
        Rect();
        void CreateSquare();
        void Draw(Canvas& canvas);
    private:
        Vector3 location_;
        Size size_;
        Image square;
};
#endif // RECT_HPP
//rect.cpp
#include "../inc/rect.hpp"

Rect::Rect()
{
    size_.width = width;
    size_.height = height;
    location_ = Vector3((double)x, (double)y);
    CreateSquare();
}

void Rect::CreateSquare()
{
    square = Image(size_.width, size_.height, Colour(255,0,255));

    for (int y = 0; y < square.getHeight(); y++)
    {
        for (int x = 0; x < square.getWidth(); x++)
        {
            square.setPixel(x, y, Colour(0,255,0));
        }
    }
}

void Rect::Draw(Canvas& canvas)
{
    canvas.blit(square, 0, 0, square.getWidth(), square.getHeight(), location_.getX(), location_.getY());
}
If you don't want a class member to be constructed with the default constructor (constructor that takes no arguments) you have to specify which constructor you want to use in the constructor initialization list.
1
2
3
4
Level::Level()
:	square(100,100,100,100)
{
}
Thanks
Last edited on
You have not defined a Rect constructor with 4 int parameters.
Topic archived. No new replies allowed.