I wrote the following classes, but there is syntax errors there, but I cannot figure out why.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
class cd_c {
public:
static physics_c phy;//physics_c is a defined class
};
class cPoint_c:public point_c, public cd_c {//error messsage:invalid use of incomplete type ‘class point_c’
public:
cellType_e T;
cPoint_c(const numeric_t & xV=0, const numeric_t & yV=0
, const cellType_e & tv=fCell)
: point_c(xV, yV)//point_c(x,y) constructor is defined, but there is error message:type ‘point_c’ is not a direct base of ‘cPoint_c’
, T(tv) {}
virtual ~cPoint_c()=default;
};
what is an incomplete type? I cannot have a type which only has static members?
please explain what is the problem here? Thanks
A class that has been declared but not defined (class point_c above) is an incomplete type.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
class point_c ; // point_c is an incomplete type here
class A : public point_c // *** error: the base class can't be of an an incomplete type
{
//...
};
class point_c
{
// ...
}; // point_c is now a complete type (the class has been defined)
class B : public point_c // fine
{
//...
};
Thanks, JLBorges! Now that comes to here, let me ask you a question about class declaration
Firstly, I did these:
in file A.h:
1 2 3 4 5 6
#ifndef A_H_
#define A_H_
class point_c {
//definition here
};
#endif
in file B.h:
1 2 3 4 5 6 7 8 9 10 11
#ifndef A_H_
#define A_H_
#inculde "C.h";//following showed pore_c, so C.h should be included
class cell_c:public point_c {
public:
//definition here
vector<pore_c *> pv;
}
#endif
in file C.h:
1 2 3 4 5 6 7 8 9 10
#ifndef A_H_
#define A_H_
#include "B.h";//following showed cell_c, so C.h should be included
class pore_c:cell_c {
public:
//definition here
vector<cell_c*> cv;
}
#endif
Because of this cross include, there will be problems. So I changed my stratge. I declared all class in a file firstly, and let all three definition header files to include it.
like this:
in file D.h:
1 2 3
class point_c;
class cell_c;
class pore_c;
and then for example
in file B.h:
1 2 3 4 5
#include "D.h"
class cell_c {
vector<pore_c *> pv;
}
head guard are added for all three files. Everything looks good.
But Now I suddenly realized, I need all classes share a static member.
so I declared this cd_c in D.h.
and defined it in B.h
1 2 3 4
class cd_c {
public:
static physics_c phy;//physics_c is a defined class
};
1 2 3 4 5 6 7 8 9 10 11
class cell_c:public point_c, public cd_c {//error messsage:invalid use of incomplete type ‘class point_c’
public:
cellType_e T;
cell_c(const numeric_t & xV=0, const numeric_t & yV=0
, const cellType_e & tv=fCell)
: point_c(xV, yV)//point_c(x,y) constructor is defined, but there is error message:type ‘point_c’ is not a direct base of ‘cell_c’
, T(tv) {}
virtual ~cell_c()=default;
};
now here comes the problem with which I initiate this question. The definition of point_c is complete I already tested. I guess the problem is in this multi inheritance. What could be worng?