Constructor for class must explicitly initialize the base class don't have..

Cnstructor for class must explicitly initialize the base class don't have default constructor.

This is the error I get from my constructor.. Could someone help resolve this problems.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Class Room: public poly{
public:
    Room(poly&);
    poly corners;
    pair<int, int> center;
};

Room::Room(poly &lo) :  corners(lo.x1,lo.y1,lo.x2,lo.y2,lo.x3,lo.y3,lo.x4,lo.y4)
// error appears hear. 
{
    center.first = min(lo.x1, lo.x4);
    center.second = min(lo.y1, lo.y2);
    distance = 0;
      
}


class poly {
public:
    int x_1, y_1, x_2, y_2, x_3, y_3, x_4, y_4;
    poly(int x_1, int y_1, int x_2, int y_2, int x_3, int y_3, int x_4, int y_4)
    :x_1(x_1),y_1(y_1),x_2(x_2),y_2(y_2),x_3(x_3),y_3(y_3),x_4(x_4),y_4(y_4){}
};
Last edited on
What are you trying to do?
closed account (SECMoG1T)


Hey.... I guess you might need .. move poly into scope . I mean put it at line 1 instead so it definition can be seen by room.

You might not really need line 4 because. ... room is a child of poly so it have all poly members variables
You can think of it like ... all Rooms will contain a poly object

On line 9 the problem is that... you aren't intializing the base class variables in the right way... maybe you might need to format you code like this

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
   class poly
       {
            public:
                   poly (const poly& rhs); ///define this copy constructor
                   poly (int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4, ) 
                     :x_1(x_1),y_1(y_1),x_2(x_2),y_2(y_2),x_3(x_3),y_3(y_3),x_4(x_4),y_4(y_4){} 
            private:
                  int x_1, y_1, x_2, y_2, x_3, y_3, x_4, y_4;
       };

   class Room: public poly
       { 
           public: 
                 Room (int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4);
                 Room(const Room& rhs); 
           private:
                 pair<int, int> center; 
                 int distance;
       };
        
        poly::poly (const poly& rhs)
         {
           ///if (rhs and this are same dont do nothing
               ///else copy it content to this.
         }
        
        Room::Room(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4)
          :poly (x1, y1, x2, y2, x3, y3, x4,y4)/// explicit intializer to base class
        { 
           center.first = min(x1, x4); /// 
           center.second = min(y1, y2);
           distance = 0;
         } 

        Room::Room(const Room & rhs) : poly (rhs)/// rhs is a poly and a Room to , I explicitly intialize
        {                                                                            /// poly members with those in rhs then my extra Room 
                                                                                     /// variables
           center.first = min(rhs.x1, rhs.x4); ///access x1 and x4 using getters. ..error! !!!
           center.second = min(rhs.y1, rhs.y2);
           distance = 0;
         } 



I hope that gives you a small idea .... try such it might not throw
Plus I dint debug this so it might have a couple flaws
Last edited on
Topic archived. No new replies allowed.