我是否被迫使用2个标题或者使用标题的方式是什么

时间:2016-04-03 06:50:41

标签: c++11 gcc forward-declaration

#include <stdio.h>
#include <string>
#include <chrono>
#include <deque>
#include <mutex>
#include <iostream>
#include <fstream>
#include <thread>
#include <atomic>
#include <utility>
#include <map>

class session_elem;
typedef std::map<std::string, session_elem> autosession;
union session_elem_t{
    std::string s;
    int64_t n;
    char c;
    std::function<void(autosession*)> f;
    autosession r;
};
class session_elem{
    enum type{ s, n, c, f, r };
    session_elem_t val;
};
typedef std::map<std::string, session_elem> autosession;

int main(int argc, char **argv)
{
    printf("hello world\n");
    return 0;
}

这个难题有解决方案吗?

根据订单的不同,我收到了前方声明错误。

我必须使用什么样的声明来使我的代码在GCC 5.3下编译?

我不能比这更小,它仍然无法编译。一切似乎都是正确的,所以为什么会有前向声明错误?

给出的错误是

/bin/sh -c '/usr/bin/make -j8 -e -f  Makefile'
----------Building project:[ ArchiUtils - Debug ]----------
make[1]: Entering directory '/home/archivist/Projets/zdd/ArchiUtils/ArchiUtils'
codelite-cc /usr/bin/g++  -c  "/home/archivist/Projets/zdd/ArchiUtils/ArchiUtils/main.cpp" -pedantic -std=c++14 -Wall -g -O0 -Wall  -o ./Debug/main.cpp.o -I. -I.
In file included from /usr/include/c++/5.3.1/bits/stl_algobase.h:64:0,
                 from /usr/include/c++/5.3.1/bits/char_traits.h:39,
                 from /usr/include/c++/5.3.1/ios:40,
                 from /usr/include/c++/5.3.1/ostream:38,
                 from /usr/include/c++/5.3.1/iostream:39,
                 from /home/archivist/Projets/zdd/ArchiUtils/ArchiUtils/main.cpp:1:
/usr/include/c++/5.3.1/bits/stl_pair.h: In instantiation of 'struct std::pair<const std::__cxx11::basic_string<char>, ArchiUtils::v1::sv0::session_elem>':
/usr/include/c++/5.3.1/functional:1981:45:   required by substitution of 'template<class _Functor, class> std::function<_Res(_ArgTypes ...)>::function(_Functor) [with _Functor = std::function<void(std::map<std::__cxx11::basic_string<char>, ArchiUtils::v1::sv0::session_elem>*)>; <template-parameter-1-2> = <missing>]'
/home/archivist/Projets/zdd/ArchiUtils/ArchiUtils/ArchiUtils.hpp:1003:10:   required from here
/usr/include/c++/5.3.1/bits/stl_pair.h:102:11: error: 'std::pair<_T1, _T2>::second' has incomplete type
       _T2 second;                /// @c second is a copy of the second object
           ^
In file included from /home/archivist/Projets/zdd/ArchiUtils/ArchiUtils/main.cpp:2:0:
/home/archivist/Projets/zdd/ArchiUtils/ArchiUtils/ArchiUtils.hpp:1003:10: note: forward declaration of 'class ArchiUtils::v1::sv0::session_elem'
    class session_elem{
          ^
ArchiUtils.mk:95: recipe for target 'Debug/main.cpp.o' failed
make[1]: *** [Debug/main.cpp.o] Error 1
make[1]: Leaving directory '/home/archivist/Projets/zdd/ArchiUtils/ArchiUtils'
Makefile:4: recipe for target 'All' failed
make: *** [All] Error 2
====4 errors, 3 warnings====

2 个答案:

答案 0 :(得分:0)

        class session_elem;
        union session_elem_t;
        enum session_elem_type{s,n,c,f,r};
        typedef std::map<std::string, session_elem> autosession;
        union session_elem_t{
            std::string s;
            int64_t n;
            char c;
            std::function<void(autosession*)> f;
            std::map<std::string, session_elem> r;
        };
        class session_elem{
            session_elem_type type;
            session_elem_t* val;
        };

通过将session_elem_t更改为session_elem_t*来制作session_elem POD,使其成功。

让魔术发生了。

答案 1 :(得分:0)

由于原始代码中的问题源于session_elem_t::f声明为std::function<void(autosession*)> f, 因为它需要定义:

std::pair<const std::__cxx11::basic_string<char>, session_elem>

session_elem未定义的时候,对您来说可能更方便 (虽然我无法从可用的代码中确定)来定义session_elem_t::f 只是一个函数指针,而不是 将session_elem::session_elem_t val替换为session_elem::session_elem_t *val。 这将编译:

#include <map>
#include <string>
#include <functional>

class session_elem;
typedef std::map<std::string, session_elem> autosession;
union session_elem_t{
    std::string s;
    int64_t n;
    char c;
    void (*f)(autosession*);
    autosession r;
};
class session_elem{
    enum type{s,n,c,f,r};
    session_elem_t val;
};