标签: forward-declaration

继承错误困境:"无效使用不完整类型"VS"预期类名"

所以我试图让班级"Herder"继承"Mob"班.但我收到的编译器错误如下:

error: invalid use of incomplete type 'struct Mob'
error: forward declaration of 'struct Mob'
Run Code Online (Sandbox Code Playgroud)

这就是Herder.h的样子:

#ifndef HERDER_H_INCLUDED
#define HERDER_H_INCLUDED

#include <SFML/Graphics.hpp>
#include <cstdlib>
#include "Level.h"
#include "Mob.h"

class Mob;

class Herder : public Mob
{
public:
    //Member functions.
    Herder(Level* level, int x, int y);
    void virtual GameCycle(sf::RenderWindow* App);
    void virtual Die();
    void Roar();
protected:
    float m_RoarCountdown;
    float m_RoarTime;
    float m_Speed;
    bool m_Roaring;
};

#endif // HERDER_H_INCLUDED
Run Code Online (Sandbox Code Playgroud)

确定它必须class Mob;是导致这种情况,我将其删除,但后来我得到以下错误,引用花括号打开的行:

error: expected class-name before '{' token
Run Code Online (Sandbox Code Playgroud)

这实际上是我最初添加前向声明的原因 …

c++ inheritance forward-declaration

2
推荐指数
1
解决办法
3669
查看次数

涉及std :: vector等的前向声明

我经常使用前瞻性声明; 它们有助于避免许多问题#include,缩短编译时间.但是如果我想在标准库中转发声明一个类呢?

// Prototype of my function - i don't want to include <vector> to declare it!
int DoStuff(const std::vector<int>& thingies);
Run Code Online (Sandbox Code Playgroud)

我听说有人禁止/无法向前宣布std::vector.现在这个无关问题的答案建议用这种方式重写我的代码:

stuff.h

class VectorOfNumbers; // this class acts like std::vector<int>
int DoStuff(const VectorOfNumbers& thingies);
Run Code Online (Sandbox Code Playgroud)

stuff.cpp

// Implementation, in some other file
#include <vector>
class VectorOfNumbers: public std::vector<int>
{
    // Define the constructors - annoying in C++03, easy in C++11
};

int DoStuff(const VectorOfNumbers& thingies)
{
    ...
}
Run Code Online (Sandbox Code Playgroud)

现在,如果我在整个项目中使用VectorOfNumbers而不是std::vector<int>在所有上下文中,一切都会很好,我不再需要#include …

c++ forward-declaration c++-standard-library c++11

2
推荐指数
1
解决办法
1461
查看次数

包含后是否需要前瞻声明?

我在自己的文件中有一个名为GameState的类,该类有一个指向另一个StatusView类型对象的指针,该对象位于自己的文件中.在GameState.h中,我已经包含了StatusView标头,但是当我尝试编译它时,我收到错误:

missing type specifier - int assumed
Run Code Online (Sandbox Code Playgroud)

但是,当我在包含它之后转发声明StatusView时,我能够编译它.我不知道是什么导致要求转发声明类.

c++ include visual-studio-2010 forward-declaration

2
推荐指数
1
解决办法
123
查看次数

C++/CLI前向声明问题

我一直在尝试进行前向声明以允许类之间的访问.我在这里读到:

  • 在向前声明b中的A时,我不能包含"ah"文件

在前向声明期间,我无法找到很多名称空间.我相当肯定我搞砸了(我只是不知道把它们放在哪里).我得到的错误是在相关代码片段之后.

这就是我做的:

  • 我已将我的类定义从.h分割为.cpp和.h

  • 我的.h文件中有#ifndef警卫

这些是我的文件:

Form1.cpp

#include "stdafx.h"
#include "OpenGL.h"
#include "Form1.h"
#include "serialcom.h"
#include "calculations.h"

using namespace GUI_1;

GUI_1::Form1::Form1(void)
    {....
    }
void GUI_1::Form1::chlabel2(float num)
    {....
    }
int GUI_1::Form1::updateHand(int source){....}
void GUI_1::Form1::resetHand(){....}
Run Code Online (Sandbox Code Playgroud)

Form1.cpp错误每个定义都是一样的

error C2872: 'GUI_1' : ambiguous symbol
could be 'GUI_1'
or       OpenGLForm::GUI_1'
Run Code Online (Sandbox Code Playgroud)

Form1.h

#ifndef form1
#define form1

using namespace OpenGLForm; 
//error C2871: 'OpenGLForm' : a namespace with this name does not exist
ref class COpenGL;

namespace GUI_1 {

    using namespace System;
    using namespace …
Run Code Online (Sandbox Code Playgroud)

c++ c++-cli forward-declaration winforms

2
推荐指数
1
解决办法
1784
查看次数

为什么VS2010中的"ambigous symbol"错误代码如下?

这段代码在VS2010中编译,我相信它适用于任何编译器.

#include <iosfwd>
using namespace std;
class ostream;
int main() {}
Run Code Online (Sandbox Code Playgroud)

这段代码也是如此

#include <iosfwd>
using namespace std;
int main() { class ostream; }
Run Code Online (Sandbox Code Playgroud)

但是此代码生成错误C2872:'ostream':模糊符号

#include <iosfwd>
using namespace std;
class ostream;
int main() { class ostream; }
Run Code Online (Sandbox Code Playgroud)

c++ stream forward-declaration

2
推荐指数
1
解决办法
1005
查看次数

c ++前向声明和不完整类型

你好,我在使用前向声明时遇到了麻烦.我无法访问转发的类函数,但我需要这样做.

这是我的Window.h:

#include "Tab.h"  // Needed because Window will create new Tabs

class Window {
  public:
    ...
    void doSome();

};
Run Code Online (Sandbox Code Playgroud)

这是Tab.h:

class Window;  // forward delcaration

class Tab {

public:
  class Tab(Window *parent);
  void callParentFunction();

private:
  Window *m_parent;
};
Run Code Online (Sandbox Code Playgroud)

最后,Tab.cpp:

#include "Tab.h"

Tab::Tab(Window *parent) {
  m_parent = parent;
}

Tab::callParentFunction() {
  m_parent->doSome();  // Error
}
Run Code Online (Sandbox Code Playgroud)

编译器返回以下错误:无效使用不完整类型'struct Window'

如何知道它已经包含Tab.h来创建标签,我如何访问父节点的功能?如果我不能,你建议我做什么?

c++ circular-dependency forward-declaration incomplete-type

2
推荐指数
1
解决办法
622
查看次数

C++中交叉依赖类的最佳实践

我有两个相互依赖的类:

// model.h
#include "facet.h"
class Model {
    ...
    std::map<int, std::vector<Facet*> > enqueue_list_;
}

// facet.h
#include "model.h"
class Facet {
    ...
    Model *parent_;
}
Run Code Online (Sandbox Code Playgroud)

编译时,编译器显然非常沮丧.我用这样的前向类声明修复了它:

// model.h
#include "facet.h"
class Facet;
class Model {
    ...
    std::map<int, std::vector<Facet*> > enqueue_list_;
}

// facet.h
#include "model.h"
class Model;
class Facet {
    ...
    Model *parent_;
}
Run Code Online (Sandbox Code Playgroud)

但我不喜欢这样做.这对我来说似乎很麻烦,很难维护.我认为通过使用头文件开始解决这类问题.有没有更好的方法来解决我错过的问题?我不想在其他类头文件中转发声明类.我觉得我在这里缺少一些重要的东西.如果我仍然遇到交叉依赖性投诉,那么头文件的确切意义何在?
提前谢谢,
马克斯

编辑

感谢您的快速回复.我接受了建议并删除了头文件中的包含,并完全切换到转发声明.我是正确的假设我仍然需要实际包含标头,如果类具有类似直接的类分配:

// facet.h
#include "point.h"
class Facet {
    ...
  private:
    Point normal_;
}
Run Code Online (Sandbox Code Playgroud)

在尝试使用前向声明时,我遇到了错误.这确实是我必须要做的情况#include吗?

c++ header class circular-dependency forward-declaration

2
推荐指数
1
解决办法
776
查看次数

函数定义在命名空间中的声明之前

下面的代码,其中函数定义预先声明其声明,在VS .NET 2008和Cygwin gcc 4.8.2中编译.这合法吗?

namespace N
{
    int init()      // Definition appears first
    {
        return 42;
    }       
}

namespace N
{
    int init();     // followed by declaration
    const int x = init();
}

int main()
{
}
Run Code Online (Sandbox Code Playgroud)

编辑

我想这与下面的编译并没有太大区别

void foo()
{
}

void foo();

int main()
{
}
Run Code Online (Sandbox Code Playgroud)

c++ namespaces forward-declaration

2
推荐指数
1
解决办法
74
查看次数

如何转发声明自定义unique_ptr

我在实现如下构造时遇到了问题.我的想法是我在一些头文件中定义了一个实用程序函数,它分配了某种资源.为了减轻客户自己释放它的必要性,我想把它包装成一个智能指针.不幸的是,我不得不将删除器类型提供给客户端,并且无法正确地向前声明它.我当前的解决方案看起来像这样,导致多个定义错误,因为删除器是使用utilities.h的每个附加包含重新定义的

这是我正在寻找的优雅解决方案?

// utilities.h
namespace foo
{
  auto del = []( MyResource* res) { Deallocate( res ); };
  std::unique_ptr<MyResource, decltype(del)> getResource( );
}

// utilities.cpp
namespace foo
{
  std::unique_ptr<MyResource, decltype(foo::del)> getResource( )
  {
    return std::unique_ptr<MyResource, decltype(foo::del)>( Allocate( ), del );
  }
}
Run Code Online (Sandbox Code Playgroud)

c++ forward-declaration unique-ptr

2
推荐指数
1
解决办法
402
查看次数

c ++多个递归函数

如何在声明之前使用函数?

我有3个函数,每个函数都必须相互交互,但我找不到一种方法来使早期声明的函数与后来声明的函数进行交互.这是代码:

int f(int a){
    if (a==0){return 1;}
    if (a==1){return 2;}
    if (a==2){return 7;}
    return 2*f(a-1)+3*f(a-2)+3*f(a-3)+6*g(a-2)+2*g(a-3)+2*h(a-2);
}

int g(int b){
    if (b==0){return 0;}
    if (b==1){return 1;}
    return f(b-1)+g(b-1)+h(b-1);
}

int h(int c){
    if (c==0){return 0;}
    if (c==1){return 1;}
    return f(c-2)+g(c-1)+g(c-2);
}
Run Code Online (Sandbox Code Playgroud)

c++ recursion function forward-declaration

2
推荐指数
1
解决办法
592
查看次数