标签: forward-declaration

使用稍后声明的 C 结构体

我想使用尚未定义的 typedef 结构,但它是稍后定义的。有没有类似结构体原型的东西?

文件容器.h

// i would place a sort of struct prototype here
typedef struct 
{
 TheType * the_type;
} Container;
Run Code Online (Sandbox Code Playgroud)

文件 thetype.h

typedef struct {......} TheType;
Run Code Online (Sandbox Code Playgroud)

文件main.c

#include "container.h"
#include "thetype.h"
...
Run Code Online (Sandbox Code Playgroud)

c coding-style typedef forward-declaration

3
推荐指数
2
解决办法
2906
查看次数

不完整类型的无效使用(嵌套类情况)

我怎样才能在 C++ 中实现这样的想法而不陷入“无效使用不完整类型”的麻烦?

class A {
    /*(...) some fields and methods here. */
    class B {
        /*(...) some fields and methods here. */
        friend B A::fun();
    };
    B fun();
};
Run Code Online (Sandbox Code Playgroud)

c++ forward-declaration incomplete-type

3
推荐指数
1
解决办法
818
查看次数

为什么允许指向不完整类型的指针而不是不完整类型的变量?

为什么以下内容是合法的:

typedef struct a aType;
struct a
{
   int x;
   aType *b;
};
Run Code Online (Sandbox Code Playgroud)

以及以下违法行为:

void main()
{
    typedef struct a aType;
    aType someVariable;
    struct a
    {
      int x;
      aType *b;
    };
}
Run Code Online (Sandbox Code Playgroud)

我只是很好奇,因为在每种情况下它都是前向引用,据我所知,至少对于函数和变量来说,前向引用是不合法的。

另外,这个问题的答案对于 C++ 来说也是一样的吗?

c typedef forward-declaration

3
推荐指数
1
解决办法
453
查看次数

使用类型作为字段时,为什么不能推迟不完整类型(前向声明)大小的计算?

在以下代码中:

class B;

struct A
{
   B* b; // <- why MUST be a pointer? Why size cannot be calculated later...?
}

struct B
{
   ...
}
Run Code Online (Sandbox Code Playgroud)

据我了解,struct A 必须将 b 定义为 B*,因为编译器在计算 A 的大小时无法告诉 B 的大小。

我不明白的是,为什么编译器不能推迟计算,直到找到 B 的完整定义(正如程序员通过转发声明类所承诺的那样)

c++ forward-declaration

3
推荐指数
1
解决办法
87
查看次数

Nlohmann::json&amp; 作为函数参数,而不在标头中包含 json.hpp

我有一个类,我想要一个使用 nlohmann::json& 作为参数的函数:

class MyClass
{
public:
    void foo(nlohmann::json& node); 
};
Run Code Online (Sandbox Code Playgroud)

但我不想将 json.hpp 文件包含在我的标头中,而只包含我的 .cpp。如何在标头中声明 nlohmann::json ?我试过:

namespace nlohmann
{
    class json;
}
Run Code Online (Sandbox Code Playgroud)

但这似乎不起作用。

c++ forward-declaration nlohmann-json

3
推荐指数
1
解决办法
869
查看次数

C++ 中的前向声明 typedef 结构

我正在使用一个遗留库,它在 Foo.h 中定义了一个结构:

typedef struct { int i; int j; } Foo

然而,在我的 C++ 程序中,我尝试在头文件中写入以下内容。

// forward-declare the struct
struct Foo;

class Bar {
  void myFunction(std::shared_ptr<Foo> foo);
};
Run Code Online (Sandbox Code Playgroud)

在我的 cpp 文件中:

#include "Foo.h"

但是,这会失败并出现错误:

使用不同类型的 typedef 重新定义('struct Foo' 与 'Foo')

执行此操作的正确语法是什么?

c c++ struct typedef forward-declaration

3
推荐指数
1
解决办法
169
查看次数

Forward在命名空间内声明全局类型

我想使用第三方库而不使用其头文件.我的代码驻留在它自己的命名空间中,因此我不能使用传统的前向声明,因为我不想污染全局命名空间.目前我有类似的东西:

3rd-party-library.h----

typedef struct {...} LibData;
void lib_func (LibData *);

my-source.h-----

namespace foo {

    /*forward declaration of LibData*/

    class Abcd {
        public:
            void ghj();
        private:
            Libdata *data_;
        };
    }//namespace foo

my-source.cpp-----
#include "my-source.h"
#include <3rd-party-library.h>

namespace foo {
    typedef ::LibData LibData;
    void Abcd::ghj() {
        //do smth with data_
        }
    }//namespace foo
Run Code Online (Sandbox Code Playgroud)

是否可以以它将驻留在命名空间中的方式转发声明全局类型?简单的typedef不起作用.

c++ forward-declaration

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

定义矢量类型时的前向声明错误?

我正在尝试尽可能多地使用前向声明来最小化编译时间.我注意到当我转发声明一个使用类似std :: vector的类作为成员对象的类时(这里的例子):

class myTypeA
class A
{
  A(){};
  ~A(){};
  std::vector<myTypeA > getTypeA();

}
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

microsoft visual studio 9.0\vc\include\vector(1112) : error C2036: 'myTypeA *' : unknown size
1>        c:\program files\microsoft visual studio 9.0\vc\include\vector(1102) : while compiling class template member function 'bool std::vector<_Ty>::_Buy(unsigned int)'
1>        with
1>        [
1>            _Ty=myTypeA
1>        ]
1>        d:\cpp\A.h(15) : see reference to class template instantiation 'std::vector<_Ty>' being compiled
1>        with
1>        [
1>            _Ty=myTypeA
1>        ]
Run Code Online (Sandbox Code Playgroud)

当我使用#include"myTypeA.h"时,这工作正常,为什么?

c++ forward-declaration

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

MySQL Connector/C++ PreparedStatement:'class sql :: PreparedStatement'的转发声明

我对c ++ Mysql连接器有一点"问题".我想用PreparedStatement.我从文档http://dev.mysql.com/doc/refman/5.1/en/connector-cpp-examples-prepared-statements.html中尝试了示例

我无法编译此代码.我仍然得到错误:

main.cpp:32:10:错误:无效使用不完整类型'class sql :: PreparedStatement'在/usr/include/mysql_connection.h:28:0中包含的文件中,来自main.cpp:4:/ usr/include /cppconn/connection.h:44:7:错误:'class sql :: PreparedStatement'的前向声明

我使用debian 7和g ++ 4.7.2.

我一直在谷歌搜索几个小时,我不知道如何解决这个问题.我会很高兴每一个建议.

我的代码:

#include <iostream>
#include <cstdlib>
#include <mysql_connection.h>
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>

int main(int argc, char** argv) {
    try{
        sql::Driver *driver;
        sql::Connection *con;
        sql::PreparedStatement  *prep_stmt;

        driver = get_driver_instance();
        con = driver->connect("tcp://127.0.0.1:3306", "username", "pass");
        // db name
        con->setSchema("aa");

        prep_stmt = con->prepareStatement("INSERT INTO test(id, name) VALUES (?, ?)");

        // this will couse compilation error
        prep_stmt->setInt(1, 1);

        // this will …
Run Code Online (Sandbox Code Playgroud)

c++ g++ prepared-statement mysql-connector forward-declaration

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

为什么我需要转发声明类foo而不是class bar,即使'foo.h'和'bar.h'都包括在内?

尝试编译此代码时出现C2143错误; 我通过前向声明来修复它class Level,但我很困惑为什么需要这个,因为我已经包含了#include Level.h,这当然Level是定义的.什么混淆了我更多,不过,是我似乎需要采取同样的措施,在同一个地方类似的声明.这是我的代码:

// global.h
// Global variables

#pragma once

#include "Level.h"
#include "view.h"
#include "ActorPlayer.h"

class Level; // Without this line C2143 (missing ; before *) is
             // generated after 'extern Level'

extern Level* gamelevel;
extern View* view;
extern bool gameover;
extern bool player_turn;
extern int game_ticks;
extern ActorPlayer* act_player;
Run Code Online (Sandbox Code Playgroud)

我的问题是,为什么我不需要一个class View;和一个class ActorPlayer;?有什么相关的区别?

如果重要,这里是Level.h:

// Level.h
// Header file for Level class

#pragma once
#include "Terrain.h" …
Run Code Online (Sandbox Code Playgroud)

c++ forward-declaration

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