C++ 错误:结构的前向声明
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9840109/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
error: forward declaration of struct
提问by Lefsler
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:
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
所有权.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:
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..
我删除了这些方法,因为如果我添加这些方法,问题会很长,抱歉,代码在 PT-BR 中。我看到问题通常是前向声明。但是我找不到问题,我看了很多论坛,但我找不到问题..
Someone can help me?
有人可以帮助我吗?
Need any other part of the code?
需要代码的任何其他部分吗?
回答by dbrank0
In Proprietario.cpp on line 36 you do something with class Motocicleta, without including full class declaration first (you only have a forward declaration).
在 Proprietario.cpp 的第 36 行中,您对类 Motocicleta 进行了一些操作,而不首先包含完整的类声明(您只有一个前向声明)。
回答by roberth-k
Either the header declaring a class should be included (#include "xxx.h"
) orthe class should be forward-declared (class xxx;
). You seem to be doing both in your headers, leading to forward-declaration aftertrue declaration, which is probably the cause of said troubles.
应包含声明类的标头 ( #include "xxx.h"
)或应向前声明类 ( class xxx;
)。您似乎在标头中同时进行了这两项操作,导致在真正声明之后进行前向声明,这可能是造成上述麻烦的原因。
回答by IronMensan
You have more #include
s 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.
你有#include
比你需要的更多的s。如果您只需要前向声明,则无需包含头文件。例如,在Proprietario.h,你只能用指针Motocicleta
,Caminhao
,Carreta
和Carro
因此,所有你需要的是正向的声明,你不需要#include "motocicleta.h"
这样你可以删除。
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.
但这并不能完全解释错误。我认为如果你简化你的标题,就会更容易追踪错误。没有看到 proprietario.cpp 以及您从问题中列出的标题中删除的任何内容,我不太确定错误的原因。