compiling error for ctor in a derived class

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
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <vector>
using namespace std;
 
enum CS{
     Polar = 0,
     Cartesian = 1     
     };
     
class point{
     public:
     char _PointName;
     int _System;
     double _x;
     double _y;
     
     public:
     point(char PointName, int System, double x, double y):_PointName(PointName), _System(System), _x(x), _y(y){;}
     point(const point& p): _PointName(p._PointName), _System(p._System), _x(p._x), _y(p._y){;}
     point& operator=(const point& p){
         _PointName = p._PointName;
         _System = p._System;
         _x = p._x;
         _y = p._y;
          }
     char& name() {return _PointName;}
     int& system() {return _System;}
     double& x() {return _x;}
     double& y() {return _y;}
     ~point(){;}
};

class Ppoint: public point{
    public:
    double _r;
    double _theta;
    
    public:    
    Ppoint(char PointName, int System, double r, double theta):    _PointName(PointName), _System(System), _r(r), _theta(theta), _x(r*cos(theta)),    _y(r*sin(theta)){;}
    Ppoint(const Ppoint& p): point(p), _r(p._r), _theta(p._theta){;}
    
    Ppoint& operator=(const Ppoint& p){
         point::operator=(p);
         _r = p._r;
         _theta = p._theta;
          }
    ~Ppoint(){;}
        
    double r(){return _r;}
    double theta(){return _theta;}
    };    


Can anyone tell me what the following compiling errors implies? Thank you!

cs2.cpp: In constructor ‘Ppoint::Ppoint(char, int, double, double)’:
cs2.cpp:42: error: class ‘Ppoint’ does not have any field named ‘_PointName’
cs2.cpp:42: error: class ‘Ppoint’ does not have any field named ‘_System’
cs2.cpp:42: error: class ‘Ppoint’ does not have any field named ‘_x’
cs2.cpp:42: error: class ‘Ppoint’ does not have any field named ‘_y’
cs2.cpp:42: error: no matching function for call to ‘point::point()’
cs2.cpp:22: note: candidates are: point::point(const point&)
cs2.cpp:21: note:                 point::point(char, int, double, double)
You can not initialize base class members in an initialization list. This is left to the base class constructor. Call the base class constructor with the values you want as part of the initializer list:
55
56
57
58
59
60
61
62
63
64
65
66
class Base
{
public:
    int x;
    Base(int X) : x(X);
}

class Derived: public Base
{
public:
    Derived() : Base(7) {}
}
Also note that you can have empty braces {} without the need to have a semicolon/empty statement in the braces. ;)
Last edited on
LB,

Following your instruction, my script works! Thank you very much!

BTW, really no need to include the redundant semicolons!
Topic archived. No new replies allowed.