2013-09-24 30 views
0

使用微軟的Visual Studio 2012的編譯器,以提升1.53,我有這樣的使用了一些工作代碼:爲什麼預解壓std :: basic_string <T> break boost :: regex?

#include <boost/regex.hpp> 

後來我加了下面兩行的頭文件是regex.hpp之前包括:

namespace std {template<class T> class basic_string<T>;} 
typedef std::basic_string<TCHAR> tstring; 

現在我得到一堆編譯錯誤,但如果在這些其他行之前包含regex.hpp,則不會出現錯誤。

許多錯誤的前幾是:

c:\program files (x86)\boost\boost_1_53\boost\regex\v4\instances.hpp(106): error C2027: use of undefined type 'std::basic_string<_Elem,_Traits,_Alloc>' 
19>   with 
19>   [ 
19>    _Elem=char16_t, 
19>    _Traits=std::char_traits<unsigned short>, 
19>    _Alloc=std::allocator<char16_t> 
19>   ] 
19>c:\program files (x86)\boost\boost_1_53\boost\regex\v4\instances.hpp(106): error C2065: 'const_iterator' : undeclared identifier 
19>c:\program files (x86)\boost\boost_1_53\boost\regex\v4\instances.hpp(106): error C2923: 'boost::match_results' : 'const_iterator' is not a valid template type argument for parameter 'BidiIterator' 
19>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(364): error C2825: '_Iter': must be a class or namespace when followed by '::' 
19>   c:\program files (x86)\boost\boost_1_53\boost\regex\v4\iterator_traits.hpp(116) : see reference to class template instantiation 'std::iterator_traits<_Iter>' being compiled 
19>   with 
19>   [ 
19>    _Iter=int 
19>   ] 
19>   c:\program files (x86)\boost\boost_1_53\boost\regex\v4\match_results.hpp(68) : see reference to class template instantiation 'boost::re_detail::regex_iterator_traits<T>' being compiled 
19>   with 
19>   [ 
19>    T=int 
19>   ] 
19>   c:\program files (x86)\boost\boost_1_53\boost\regex\v4\instances.hpp(106) : see reference to class template instantiation 'boost::match_results<BidiIterator>' being compiled 
19>   with 
19>   [ 
19>    BidiIterator=int 
19>   ] 
19>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(364): error C2039: 'iterator_category' : is not a member of '`global namespace'' 
19>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(364): error C2146: syntax error : missing ';' before identifier 'iterator_category' 
19>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(364): error C2602: 'std::iterator_traits<_Iter>::iterator_category' is not a member of a base class of 'std::iterator_traits<_Iter>' 
19>   with 
19>   [ 
19>    _Iter=int 
19>   ] 
19>   c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(364) : see declaration of 'std::iterator_traits<_Iter>::iterator_category' 
19>   with 
19>   [ 
19>    _Iter=int 
19>   ] 
19>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(364): error C2868: 'std::iterator_traits<_Iter>::iterator_category' : illegal syntax for using-declaration; expected qualified-name 
19>   with 
19>   [ 
19>    _Iter=int 
19>   ] 
19>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(365): error C2825: '_Iter': must be a class or namespace when followed by '::' 
19>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(365): error C2039: 'value_type' : is not a member of '`global namespace'' 
19>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(365): error C2146: syntax error : missing ';' before identifier 'value_type' 
19>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(365): error C2602: 'std::iterator_traits<_Iter>::value_type' is not a member of a base class of 'std::iterator_traits<_Iter>' 

請誰能告訴我什麼是錯我的std :: basic_string的的predeclaration?

+0

您不允許將聲明添加到'std'命名空間。 – Simple

回答

3

請誰能告訴我我的的聲明有什麼問題?

它有三個模板參數:

template<class charT, class traits = char_traits<charT>, 
    class Allocator = allocator<charT> > 
class basic_string; 

但沒有理由給自己申報標準類型,正如你已經證明,一個很好的理由不這樣做。所以不要那樣做;如果您需要聲明,請包含標題。

+0

是的,你是對的@Mike,改變預先聲明以解決問題。沒有其他人抱怨我簡化的預先聲明,但很顯然,正則表達式有些奇怪。 –

+0

@Coder_Dan:請閱讀答案的最後一行;也許我應該讓它脫穎而出。沒有理由聲明標準類型,並且正如你的錯誤所表明的那樣,沒有理由這麼做。 (正則表達式頭不需要做任何奇怪的事情;任何包括你的聲明和真正的聲明都將失敗)。 –

+0

謝謝@Mike - 是的,我讀了這行。我質疑爲什麼這是另一個(接近重複)的答案,並解釋了爲什麼我試圖預先聲明它。我經常預先聲明一些東西來減少頭文件依賴性。 –

7

您從聲明中省略了幾個模板參數。正確的一個看起來像:

template<class charT, class traits = char_traits<charT>, 
     class Allocator = allocator<charT> > 
class basic_string; 

不要試圖自己做這件事。使用<string>

+2

最後一句沒有足夠的重點,+1 –

+0

謝謝@Jerry,是的,改變了這個固定錯誤的預先聲明。沒有其他人抱怨我簡化的預先聲明,我無法理解它是如何導致這些錯誤的,但顯然它可以。 –

+0

通過預先聲明'basic_string'的方式是什麼問題?我們最近纔開始在我們的一些源文件中使用標準庫,所以我不願意在我們的源代碼庫中傳播std :: string的知識(僅僅因爲我需要添加一些採用std ::通常包含的標題中的字符串參數)。 –

相關問題