Error: Forward Structure Declaration
I am getting the error:
proprietario.cpp:36: error: invalid use of incomplete type ‘struct Motocicleta’
proprietario.h:12: error: forward declaration of ‘struct Motocicleta’
Motocicleta.h:
#ifndef __MOTOCICLETA__
#define __MOTOCICLETA__
#include <iostream>
#include "veiculo.h"
#include "proprietario.h"
using namespace std;
class Proprietario;
class Motocicleta:public Veiculo{
public:
Motocicleta(int nPassageiros, string modelo, string placa, int aFabricacao, Proprietario* pai, int nRodas, int aro);
~Motocicleta();
Motocicleta (const Motocicleta& source);
Motocicleta& operator= (const Motocicleta& source);
string toString();
};
#endif
Proprietario.h
#ifndef __PROPRIETARIO__
#define __PROPRIETARIO__
#include <iostream>
#include "motocicleta.h"
#include "caminhao.h"
#include "carreta.h"
#include "carro.h"
using namespace std;
class Carro;
class Carreta;
class Caminhao;
class Motocicleta;
class Proprietario{
protected:
string nome;
string cpf;
Motocicleta* mMoto;
Caminhao* mCaminhao;
Carreta* mCarreta;
Carro* mCarro;
};
Veiculo.h:
#ifndef __VEICULO__
#define __VEICULO__
#include <iostream>
#include "proprietario.h"
#include "roda.h"
#include "motor.h"
using namespace std;
class Motor;
class Proprietario;
class Veiculo{
protected:
int nPassageiros;
string modelo;
string placa;
int aFabricacao;
Proprietario* pai;
Roda* rodas;
Motor* mMotor;
int nRodas;
};
I removed the methods because if I added them the question would be long, sorry, the code is in PT-BR. I've seen that the problem is usually with the forward declaration. But I cannot find the problem, I have looked at so many forums, but I cannot seem to solve the problem.
Can anyone help me?
Is there any other piece of code needed?
In Proprietario.cpp on line 36, you do something with the Motocicleta class, not including the full class declaration first (you only have the forward declaration).
A header declaring the class ( #include "xxx.h"
) must be included , or the class must be declared ahead ( class xxx;
). You seem to be doing both in your headers, resulting in a forward declaration after the true declaration, which is probably the cause of the stated problems.
You have more #include
than you need. If you only want the forward declaration, there is no need to include the header file. For example, in Proprietario.h you only use pointers to Motocicleta
, Caminhao
, Carreta
and Carro
, so all you need is a cutting-edge ads, you do not need #include "motocicleta.h"
, so you can remove it.
This doesn't quite explain the error. I think if you simplify your headers it will be easier to track down the error. Without seeing proprietario.cpp and everything you removed from the headers listed in your question, I can't be sure about the reason for the error.