2010-05-18 90 views
2

我有一个程序使用了tr1::regex,编译时它给了我非常详细的链接器错误。使用tr1 :: regex时出现链接器错误

这里是我的头文件MapObject.hpp:

#include <iostream> 
#include <string> 
#include <tr1/regex> 
#include "phBaseObject.hpp" 
using std::string; 

namespace phObject 
{ 
    class MapObject: public phBaseObject 
    { 
     private: 
      string color; // must be a hex string represented as "#XXXXXX" 
      static const std::tr1::regex colorRX; // enforces the rule above 
     public: 
      void setColor(const string&); 
     (...) 
    }; 
} 

这里是我的实现:

#include <iostream> 
#include <string> 
#include <tr1/regex> 
#include "MapObject.hpp" 
using namespace std; 


namespace phObject 
{ 
    const tr1::regex MapObject::colorRX("#[a-fA-F0-9]{6}"); 

    void MapObject::setColor(const string& c) 
    { 
     if(tr1::regex_match(c.begin(), c.end(), colorRX)) 
     { 
      color = c; 
     } 
     else cerr << "Invalid color assignment (" << c << ")" << endl; 
    } 

    (...) 
} 

,现在的错误:

最大@最大的桌面:〜/ Desktop/Development/CppPartyHack/PartyHack/lib $ g ++ -Wall -std = C++ 0x MapObject.cpp
/tmp/cce5gojG.o :函数std::tr1::basic_regex<char, std::tr1::regex_traits<char> >::basic_regex(char const*, unsigned int)': MapObject.cpp:(.text._ZNSt3tr111basic_regexIcNS_12regex_traitsIcEEEC1EPKcj[std::tr1::basic_regex<char, std::tr1::regex_traits<char> >::basic_regex(char const*, unsigned int)]+0x61): undefined reference to std :: tr1 :: basic_regex> :: _ M_compile()'
/tmp/cce5gojG.o:在函数bool std::tr1::regex_match<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, char, std::tr1::regex_traits<char> >(__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::tr1::basic_regex<char, std::tr1::regex_traits<char> > const&, std::bitset<11u>)':
MapObject.cpp:(.text._ZNSt3tr111regex_matchIN9__gnu_cxx17__normal_iteratorIPKcSsEEcNS_12regex_traitsIcEEEEbT_S8_RKNS_11basic_regexIT0_T1_EESt6bitsetILj11EE[bool std::tr1::regex_match<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, char, std::tr1::regex_traits<char> >(__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::tr1::basic_regex<char, std::tr1::regex_traits<char> > const&, std::bitset<11u>)]+0x53): undefined reference to
bool std :: tr1 :: regex_match < __gnu_cxx :: __ normal_iterator,std :: allocator>> ,std :: allocator,std :: allocator>>>,char,std :: tr1 :: regex_traits>(__ gnu_cxx :: __ normal_iterator,std :: allocator>>,__gnu_cxx :: __ normal_iterator,std :: allocator>>, std :: tr1 :: match_results < __gnu_cxx :: __ normal_iterator,std :: allocator>>,std :: allocator,std :: allocator> >>> &,std :: tr1 :: basic_regex> const &,std :: bitset < 11u>)'
collect2:ld返回1退出状态

我真的不能真正做到这一点,除了undefined reference to std::tr1::basic_regex开始附近。任何人都知道发生了什么事?

+1

这不断出现。我真的希望海湾合作委员会会停止分发正则表达式头,所以人们会得到理解的编译时错误,而不是不可理解的链接时间错误。 – 2010-05-18 20:34:06

+0

这个,或者至少在其中加入了一个描述性的“#error”。 – 2010-05-18 20:39:53

回答

0

答案是,即使提供了头文件,也不提供某些方法。

人们可以从乔治的答案中推断出这一点,但是根据假设实际上提供了漂亮的图书馆来思考并编码了一百行左右的代码后,人们可能对于任何进一步的推论都太累了。

相关问题