我有一个使用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;
}
(...)
}
现在是错误:
max @ max-desktop:〜/ 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&gt; :: _ 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&lt; __ gnu_cxx :: __ normal_iterator,std :: allocator&gt; &gt;,std :: allocator,std :: allocator&gt; &GT; &GT; &gt;,char,std :: tr1 :: regex_traits&gt;(__ gnu_cxx :: __ normal_iterator,std :: allocator&gt;&gt ;, __gnu_cxx :: __ normal_iterator,std :: allocator&gt;&gt ;,std :: tr1 :: match_results&lt ; __ gnu_cxx :: __ normal_iterator,std :: allocator&gt;&gt;,std :: allocator,std :: allocator&gt;&gt;&gt;&gt;&gt;&amp;,std :: tr1 :: basic_regex&gt; const&amp;,std ::位集&LT; 11U&GT)'
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
之外,我无法真正做到这一点。有谁知道发生了什么事?
答案 0 :(得分:1)
正则表达式对C ++ 0x的支持不完整,TR1不支持,请参阅implementation status page了解C ++ 0x / TR1。
Boost提供了另一种TR1 implementation以及它所基于的original library。
答案 1 :(得分:0)
答案是,即使提供了标题,也不提供某些方法。
有人可以从乔治的回答中推断出这一点,但是在根据假设实际提供了漂亮的图书馆的想法并编写了一百行之后,人们可能会因为任何进一步的扣除而感到疲惫。