草庐IT

forward-declaration

全部标签

c++ - 带有类类型 vector 的前向声明 - 不允许指向不完整类类型的指针

我有两个类,foo和bar。foo.h#includesbar.h并包含指向bar对象的指针的std::vector。在运行时的某个时刻,bar必须访问这个指向其他bar对象的指针vector。因此,foo包含一个名为getBarObjects()的方法,该方法返回指针数组。因此,我在bar.h中转发声明foo。显然,我还必须转发声明我正在使用的方法-foo::getBarObjects()。由于这会返回指向bar的指针数组,因此我陷入了恶性循环。我不能转发声明Bar然后简单地转发声明getBarObjects(),因为这会导致“不允许不完整的类型名称”。foo.h:#include"

c++ - g++ 6.1 可能的 std::forward 回归 - 错误或预期行为?

g++6.1最近被引入到ArchLinux的测试库中,我的一些使用g++5.3.0成功编译的代码不再编译。我做了一个最小的例子:gcc.godbolt.orglink//Thiscodecompileswithg++5.3.0//Thisdoesnotcompilewithg++6.1#include#include#include#defineFWD(...)::std::forward(__VA_ARGS__)structsinker{templatevoidsink(T&){}};templatevoidcaller(T&v,TF&&f){sinkers;f(s,v);}temp

c++ - "indirectly declaring a namespace within another namespace"是什么意思?

[basic.link]C++14标准中的第4段:Anunnamednamespaceoranamespacedeclareddirectlyorindirectlywithinanunnamednamespacehasinternallinkage.Allothernamespaceshaveexternallinkage.Anamehavingnamespacescopethathasnotbeengiveninternallinkageabovehasthesamelinkageastheenclosingnamespaceifitisthenameof...上面的“在另一个命名

c++ - 为什么没有 <STLfwd> header ,它的不存在可以被视为缺陷吗?

标准库包含一个header,(向前)声明所有流,包括任何typedefs并定义了char_traits模板,包括专业。遗憾的是,没有这样的header(向前)声明所有常见的STL数据类型和函数,如vector,map,less,sort等等。更可悲的是,用户代码不允许添加这样的声明/typedefs到std命名空间,根据§17.4.3.1[lib.reserved.names]p1:ItisundefinedforaC++programtoadddeclarationsordefinitionstonamespacestdornamespaceswithinnamespacestdun

c++ - 前向声明类型上的 std::vector

以下代码似乎可以在Clang++和GCC上正常工作:#includeclassA{private:inti;std::vectorchildren;public:A&add();};A&A::add(){children.emplace_back();returnchildren.back();}intmain(){Aa;A&a2=a.add();}当数据成员std::vector已声明,A仍然是一个不完整的类型。使用std::vector时相同和B仅使用classB;向前声明.它应该与std::vector一起使用因为它只包含一个指向-A的指针.这是保证有效,还是未定义的行为?

c++ - 如何转发声明应该属于一个类的模板化类型?

假设我有2个类:classA{public:typedefstd::shared_ptrRef;...private:B::Ref_b;}classB{public:typedefstd::shared_ptrRef;...private:A::Ref_a;}这显然需要B类和B::Ref的前向声明。前向声明B很简单,但是如何为B::Ref做到这一点呢? 最佳答案 解决这个问题的方法之一是classA;classB;templatestructTraits{typedefstd::shared_ptrPtr;};classA{priv

c++ - 枚举转发声明

这个问题在这里已经有了答案:ForwarddeclaringanenuminC++(19个回答)关闭9年前。我正在尝试正确使用枚举的前向声明。因此,我搜索了Internet,但找不到有用的东西。我在标题中使用它://ForwarddeclarationenummyEnumProcessState;然后我在结构中使用这个枚举:structmyStruct{[...]myEnumProcessStateosState;[...]};在另一个标题中:enummyEnumProcessState{eNotRunning,eRunning};我发现应该将类型放在要接受的枚举前向声明中。但是,我不

C++ "was not declared in this scope"编译错误

C++新手。在我编写的以下程序中出现此错误:g++-oBlobblob.ccblob.cc:Infunction'intnonrecursivecountcells(color(*)[7],int,int)':blob.cc:41:error:'grid'wasnotdeclaredinthisscope代码如下:#includeenumcolor{BACKGROUND,ABNORMAL,TEMPORARY};constintROW_SIZE=7;constintCOL_SIZE=7;intnonrecursivecountcells(color[ROW_SIZE][COL_SIZE]

c++ - 当您不想 #include 时,可以替代前向声明

我通常不假思索地使用前向声明,这样我就不必包含标题。这个例子中的一些东西://-----------------------//foo.h//-----------------------classfoo{foo();~foo();};//-----------------------//bar.h//-----------------------classfoo;//forwarddeclarationclassbar{bar();~bar();foo*foo_pointer;};一些开发者喜欢使用这种方法来避免包含环的问题。我宁愿使用它来最大限度地减少广泛包含层次结构的开销,这是物

C++ Forward方法调用以在没有继承的情况下嵌入对象

我想知道是否有可能自动将方法调用转发给嵌入的对象,没有继承。例如:classembed{public:voidembed_method(){return};};classcontainer{public:voidcontainer_method(){return;}private:embedobj;};intmain(){containerobject;object.container_method();//Localmethodcallobject.embed_method();//'Forward'call,obviouslydoesn'twork}当不可能/不推荐从基类继承时,它