S
sam
Hi,
I can't figure out what is the problem of the following coding.
#ifndef __PARSER__
#define __PARSER__
#include <iostream>
#include <string>
#include <fstream>
//#include <exception>
#include <iomanip> // Header for I/O stream manipulators
#include <ext/hash_map>
#include <vector>
#include <list>
#define LLEN 256
// Namespace alias to reach hash_map classes
namespace stdext = ::__gnu_cxx;
using namespace std;
using namespace stdext;
/* struct or */
class HashString
{
public:
/*long*/ int operator()(std::string const &str) const
{
return stdext::hash<char const *>()(str.c_str());
}
};
// This class' function operator() tests if any two keys are equal.
/* struct or */
class HashStringCompare
{
public:
bool operator()(std::string s1, std::string s2) const
{
return s1 == s2;
}
};
class HashMap: public hash_map<string, string, HashString,
HashStringCompare>
{
public:
HashMap(): hash_map<string, string, HashString, HashStringCompare>() {}
};
#define SPACES " \t\n\r" // default "spaces" for trimming strings
class Parser
{
public:
Parser(string &cmd);
Parser(ifstream &f);
virtual int parse() {return 1;};
void _debug(list<HashMap> &l);
// trim spaces from right (you can define what spaces are)
string trim_right (const string & s, const string & t = SPACES);
// trim spaces from left
string trim_left (const string & s, const string & t = SPACES);
// trim spaces from both sides
string trim (const string & s, const string & t = SPACES);
virtual ~Parser() {};
protected:
vector<string> v_data;
};
inline string trim_right (const string & s, const string & t)
{
string d (s);
string::size_type i (d.find_last_not_of (t));
if (i == string::npos)
return "";
else
return d.erase (d.find_last_not_of (t) + 1) ;
} // end of trim_right
inline string trim_left (const string & s, const string & t)
{
string d (s);
return d.erase (0, s.find_first_not_of (t)) ;
} // end of trim_left
inline string trim (const string & s, const string & t)
{
string d (s);
return trim_left (trim_right (d, t), t) ;
} // end of trim
inline Parser:arser(string &cmd)
{
const char *s = cmd.c_str();
string str = trim(s);
if (str.c_str()[0] == '#' || str.length() == 0)
cout << "this is comment: " << str << endl;
else
v_data.push_back(str);
}
int main()
{
string s = " test string ";
Parser p(s);
}
#endif
When compiled with g++ in FreeBSD 5.4, it generated error shown as follow:
# g++ p_test.cpp
/var/tmp//ccJnnMuA.o(.gnu.linkonce.t._ZN6ParserC1ERSs+0xd8): In function
`Parser:arser(std::string&)':
: undefined reference to `Parser::trim(std::string const&, std::string
const&)'
I can't figure out what is the problem of the following coding.
#ifndef __PARSER__
#define __PARSER__
#include <iostream>
#include <string>
#include <fstream>
//#include <exception>
#include <iomanip> // Header for I/O stream manipulators
#include <ext/hash_map>
#include <vector>
#include <list>
#define LLEN 256
// Namespace alias to reach hash_map classes
namespace stdext = ::__gnu_cxx;
using namespace std;
using namespace stdext;
/* struct or */
class HashString
{
public:
/*long*/ int operator()(std::string const &str) const
{
return stdext::hash<char const *>()(str.c_str());
}
};
// This class' function operator() tests if any two keys are equal.
/* struct or */
class HashStringCompare
{
public:
bool operator()(std::string s1, std::string s2) const
{
return s1 == s2;
}
};
class HashMap: public hash_map<string, string, HashString,
HashStringCompare>
{
public:
HashMap(): hash_map<string, string, HashString, HashStringCompare>() {}
};
#define SPACES " \t\n\r" // default "spaces" for trimming strings
class Parser
{
public:
Parser(string &cmd);
Parser(ifstream &f);
virtual int parse() {return 1;};
void _debug(list<HashMap> &l);
// trim spaces from right (you can define what spaces are)
string trim_right (const string & s, const string & t = SPACES);
// trim spaces from left
string trim_left (const string & s, const string & t = SPACES);
// trim spaces from both sides
string trim (const string & s, const string & t = SPACES);
virtual ~Parser() {};
protected:
vector<string> v_data;
};
inline string trim_right (const string & s, const string & t)
{
string d (s);
string::size_type i (d.find_last_not_of (t));
if (i == string::npos)
return "";
else
return d.erase (d.find_last_not_of (t) + 1) ;
} // end of trim_right
inline string trim_left (const string & s, const string & t)
{
string d (s);
return d.erase (0, s.find_first_not_of (t)) ;
} // end of trim_left
inline string trim (const string & s, const string & t)
{
string d (s);
return trim_left (trim_right (d, t), t) ;
} // end of trim
inline Parser:arser(string &cmd)
{
const char *s = cmd.c_str();
string str = trim(s);
if (str.c_str()[0] == '#' || str.length() == 0)
cout << "this is comment: " << str << endl;
else
v_data.push_back(str);
}
int main()
{
string s = " test string ";
Parser p(s);
}
#endif
When compiled with g++ in FreeBSD 5.4, it generated error shown as follow:
# g++ p_test.cpp
/var/tmp//ccJnnMuA.o(.gnu.linkonce.t._ZN6ParserC1ERSs+0xd8): In function
`Parser:arser(std::string&)':
: undefined reference to `Parser::trim(std::string const&, std::string
const&)'