在:http://www.learncpp.com/cpp-tutorial/19-header-files/
提到以下内容:
add.cpp:
int add(int x, int y)
{
return x + y;
}
Run Code Online (Sandbox Code Playgroud)
main.cpp中:
#include <iostream>
int add(int x, int y); // forward declaration using function prototype
int main()
{
using namespace std;
cout << "The sum of 3 and 4 is " << add(3, 4) << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我们使用了前向声明,以便编译器在编译时知道"
add"是什么main.cpp.如前所述,为要在其他文件中使用的每个函数编写前向声明可能会很快变得乏味.
你能进一步解释" 前瞻性宣言 "吗?如果我们在main()函数中使用它会有什么问题?
关于如何避免头文件的循环依赖,你有什么好建议吗?
当然,从一开始,我就尝试尽可能透明地设计项目.但是,随着越来越多的功能和类的添加,以及项目变得不那么透明,循环依赖开始发生.
是否有任何通用,经过验证和工作的规则?谢谢.
我知道有一些类似的问题(循环包括)stackoverflow和其他网站.但我仍然无法弄明白,也没有解决方案.所以我想发布我的具体内容.
我有一个Event类,它有2个实际上更多的子类,它们是Arrival和Landing.编译器(g ++)抱怨:
g++ -c -Wall -g -DDEBUG Event.cpp -o Event.o
In file included from Event.h:15,
from Event.cpp:8:
Landing.h:13: error: expected class-name before ‘{’ token
make: *** [Event.o] Error 1
Run Code Online (Sandbox Code Playgroud)
人们说这是一个循环包括.3个头文件(Event.h Arrival.h Landing.h)如下:
Event.h:
#ifndef EVENT_H_
#define EVENT_H_
#include "common.h"
#include "Item.h"
#include "Flight.h"
#include "Landing.h"
class Arrival;
class Event : public Item {
public:
Event(Flight* flight, int time);
virtual ~Event();
virtual void occur() = 0;
virtual string extraInfo() = 0; // extra info for each concrete event
// @implement …Run Code Online (Sandbox Code Playgroud) 所以我有一个类包含在另一个类中,该类不断抛出"错误:'ProblemClass'形式的编译错误.文件是这样设置的:
#ifndef PROBLEMCLASS_H
#define PROBLEMCLASS_H
#include <iostream>
#include <cmath>
class ProblemClass
{
public:
virtual void Init() = 0;
};
#endif
Run Code Online (Sandbox Code Playgroud)
并且发生错误的类看起来像这样:
#ifndef ACLASS_H
#define ACLASS_H
#include "problemclass.h"
class AClass : public Base
{
public:
void DoSomething(ProblemClass* problem);
};
#endif
Run Code Online (Sandbox Code Playgroud)
编译错误发生在void Dosomething();
我知道这里的代码不足以解决问题.我一直无法创建一个可以重现它的最小例子.所以我的问题更为笼统; 什么样的事情可能会导致这种情况?有什么特别的东西我应该寻找,或者我应该追踪一些调查线来跟踪它?
此代码在几乎完全相同的项目版本中编译良好.
无论多么模糊,都会非常感谢任何形式的帮助.我在win 7 64位中使用了mingw4.4.1的代码块10.05.
我有两节课,foo和bar.
foo.h #includes bar.h并包含一个std::vector指向bar对象的指针.在运行时期间的某个时刻,bar必须访问指向其他bar对象的指针向量.因此,foo包含一个getBarObjects()返回指针数组的方法.
因此,我foo在bar.h中转发声明.我显然还要转发声明我正在使用的方法 - foo::getBarObjects().当这返回指针数组时bar,我陷入了恶性循环.
我无法转发声明Bar然后只是转发声明getBarObjects(),因为这导致"不允许不完整的类型名称".
foo.h中:
#include "bar.h"
#include <vector>
class foo {
public:
foo();
~foo();
std::vector<bar*> getBarObjects();
private:
std::vector<bar*> barObjects;
}
Run Code Online (Sandbox Code Playgroud)
bar.h:
class foo;
std::vector<bar*> foo::getBarObjects(); // error, doesn't know bar at this point
class bar {
public:
bar(foo *currentFoo);
~bar();
bool dosth();
private:
foo *thisFoo;
}
Run Code Online (Sandbox Code Playgroud)
bar.cpp:
#include "bar.h"
bool bar(foo …Run Code Online (Sandbox Code Playgroud) 我有这个错误:
"错误C4430:缺少类型说明符 - 假定为int.注意:C++不支持default-int"
使用此代码示例:
//A.h
#include "B.h"
class A{
B* b;
..
};
//B.h
#include "A.h"
class B{
A* a; // error error C4430: missing type specifier - int assumed.
};
Run Code Online (Sandbox Code Playgroud) 我是C++的新手,并在标题中提出了问题.或者更确切地说:如果Ah包括Bh和Bh包含Ah,我会得到一条错误消息,因为"include#file"C:... \啊"包括它自己".档案:Bh
我找不到解决这个问题的方法,而我的一般设置几乎要求这些类之间的关系.是否有可能使这项工作?
所以我正在创建一个涉及将矢量嵌套在另一个内部的项目,但该结构将无法编译.我认为这是循环依赖的情况,但我看到的所有修复似乎只适用于涉及头文件和单独翻译单元的代码.
#include <iostream>
#include <vector>
class A {
public:
virtual ~A();
};
// The following forward declaration statements didn't solve the
// compiler error:
// class C;
// class C : public A;
class B : public A {
std::vector<A*> Avector;
public:
void addC(C* Cin){Avector.push_back(Cin);}
~B();
};
class C : public A {
std::vector<A*> Avector;
public:
void addB(B* Bin){Avector.push_back(Bin);}
~C();
};
int main() {
B b;
C c;
b.addC(&c);
c.addB(&b);
}
Run Code Online (Sandbox Code Playgroud)
我试着向前宣布
class C;
Class C : public A;
Run Code Online (Sandbox Code Playgroud)
但它似乎都没有用. …
我知道之前已经提出了类似的问题,但在做完我的研究之后,我仍然对循环标题包含问题.
//FooA.h
#ifndef H_FOOA
#define H_FOOA
#include "foob.h"
class FooA{
public:
FooB *fooB;
};
//FooB.h
#ifndef H_FOOB
#define H_FOOB
class FooA;
class FooB{
public:
FooA *fooA;
};
Run Code Online (Sandbox Code Playgroud)
现在,如果我有两个循环依赖项,这就是我在stackoverflow上看到人们解决问题的方式.我唯一的问题是在我的main.cpp中我必须先包含fooa.h然后再包含foob.h
//main.cpp the right way
#include "fooa.h"
#include "foob.h"
//main.cpp that will surely get a compile error
#include "foob.h"
#include "fooa.h"
Run Code Online (Sandbox Code Playgroud)
现在我的问题是"有没有办法以某种方式转发声明这些类,这将使我不关心我在main.cpp中包含头文件的顺序?"
我有此头文件,并且正在尝试创建type变量Item。我已经包含了#include "Item.h",但是unknown type name Item在编译时两个私有变量仍然出现错误。
#ifndef PLAYER_H
#define PLAYER_H
#include <vector>
#include "Item.h"
using std::vector;
class Player
{
public:
// constructor
Player( void );
// destructor
virtual ~Player( void );
private:
Item item;
std::vector <Item> inventory;
};
#endif /* PLAYER_H */
Run Code Online (Sandbox Code Playgroud)
这是怎么回事?
Item.h我包括的继承人
#ifndef ITEM_H
#define ITEM_H
#include <string>
#include "Player.h"
#include "GlobalDefs.h"
class Item {
public:
Item();
Item(gold_t v, std::string n);
virtual ~Item();
// Getter
inline virtual gold_t GetValue (void)
{
return …Run Code Online (Sandbox Code Playgroud)