Lefsler
Lefsler

Reputation: 1768

error: forward declaration of struct

I'm receiving 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 those the question will be to long, sorry, the code is in PT-BR. I saw that the problem is usually is forward declaration. But i cannot find out the problem, i looked in so many forums but i cannot find out the problem..

Someone can help me?

Need any other part of the code?

Upvotes: 5

Views: 49799

Answers (3)

IronMensan
IronMensan

Reputation: 6831

You have more #includes than you need. If you only need a forward declaration, there is no need to include the header file as well. For example, in Proprietario.h, you only use pointers to Motocicleta, Caminhao, Carreta and Carro so all you need is the forward declarations, you don't need to #include "motocicleta.h" so you can remove that.

This doesn't quite explain the error though. I think if you simplify your headers, it will be easier to track down the error. Without seeing proprietario.cpp and whatever you removed from the headers you listed in your question, I can't be too sure about the cause of the error.

Upvotes: 2

dbrank0
dbrank0

Reputation: 9476

In Proprietario.cpp on line 36 you do something with class Motocicleta, without including full class declaration first (you only have a forward declaration).

Upvotes: 12

roberth-k
roberth-k

Reputation: 94

Either the header declaring a class should be included (#include "xxx.h") or the class should be forward-declared (class xxx;). You seem to be doing both in your headers, leading to forward-declaration after true declaration, which is probably the cause of said troubles.

Upvotes: 3

Related Questions