The terminal tells me
error: ‘PointRn’ has not been declared
bool contient(PointRn p);
And that is the problem; the type PointRn
has not been declared.
Declaration.
In C ++ (and several other languages) until a type is declared, the type does not exist. How do you declare a type?:
[class / struct / union / enum ] nombre ;
As soon as a type keyword appears, such as class
followed by the name of the type, for example PointRn
, we consider that the type is declared: we are telling the compiler:
-
There is a type called
PointRn
that you might use later .
That of later is very important, the statements are read from ↑ up to ↓ below. So if we want to use a type, we must have declared it in previous lines . Therefore, in your code:
/* 01 */ class Bola{
/* 02 */
/* 03 */ private:
/* 04 */ int dim;
/* 05 */ double* adr;
/* 06 */
/* 07 */ public:
/* 08 */ bool contiene(PuntoRn p);
/* 09 */ virtual void Message() = 0;
/* 10 */ };
In line 8 you are using a type called PuntoRn
that has not been declared in any previous line, to solve it you have two solutions:
First option: Change the declaration order.
class PuntoRn{
public:
PuntoRn(int dim, double* adr);
PuntoRn(const PuntoRn &);
// Comment fait on un destructeur ?
~PuntoRn(){
std::cout << "Destructor de PuntoRn.\n";
}
private:
int dim;
double* adr;
};
class Bola{
private:
int dim;
double* adr;
public:
bool contiene(PuntoRn p);
virtual void Message() = 0;
};
You can see that we now tell the compiler that PuntoRn
exists before it is used as a parameter in Bola::contiene(PuntoRn)
.
Second option: Pre-declaration.
C ++ allows to separate the declaration of a type from the type definition. The statement we have already seen how it is done:
// class nombre;
class PuntoRn;
With that we warn the compiler that there is a type with the name PuntoRn
, but we do not say anything else: we have not indicated how it is composed, we have not written any member function nor member variables ... in summary: no we have defined it. But despite not being defined, being declared can be used in some contexts:
/* 01 */ class PuntoRn; // Declaración de PuntoRn
/* 02 */
/* 03 */ class Bola{
/* 04 */
/* 05 */ private:
/* 06 */ int dim;
/* 07 */ double* adr;
/* 08 */
/* 09 */ public:
/* 10 */ bool contiene(PuntoRn p); // Uso de PuntoRn
/* 11 */ virtual void Message() = 0;
/* 12 */ };
/* 13 */
/* 14 */ class PuntoRn{ // Definición de PuntoRn
/* -- */ ...
/* 22 */ };
Declare a type and use it without defining it, it is known in C ++ as Pre-declaration (forward declaration in English). The Pre-declaration allows you to use the type without having defined it, as long as the type is not instanced.