2016-04-03 47 views
2
#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; 
} 

難道有解決方案嗎?我被迫使用2個標頭或使用哪一種方式

根據順序,我得到一個前向聲明錯誤。

我的代碼在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==== 
+0

你在GCC 5.3編譯得很好的代碼。問題是什麼? – Rufflewind

+0

它給了我一個錯誤前向聲明......看我更新 –

+0

也在GCC 5.3上,我可以確認你上面發佈的代碼在-std = C++上編譯時沒有錯誤或警告14 -Wall -Wextra - 迂腐。你得到的具體錯誤信息是什麼,它有可能與這段代碼沒有實際關係? – acwaters

回答

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_tsession_elem_t*使session_elem POD工作。

使魔法發生。

0

作爲原密碼的麻煩從session_elem_t::f宣佈爲std::function<void(autosession*)> f, 莖,因爲它需要一個定義:在當session_elem是不確定的一個點

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

,它可能是更方便您 (雖然我不能確定從可用的代碼)定義session_elem_t::f,而不是 只是一個函數指針,而不是 取代session_elem::session_elem_t valsession_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; 
}; 
相關問題