我有一個程序使用了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>)':
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>)'
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
collect2:ld返回1退出狀態
我真的不能真正做到這一點,除了undefined reference to std::tr1::basic_regex
開始附近。任何人都知道發生了什麼事?
這不斷出現。我真的希望海灣合作委員會會停止分發正則表達式頭,所以人們會得到理解的編譯時錯誤,而不是不可理解的鏈接時間錯誤。 – 2010-05-18 20:34:06
這個,或者至少在其中加入了一個描述性的「#error」。 – 2010-05-18 20:39:53