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::Parser(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::Parser(std::string&)':
: undefined reference to `Parser::trim(std::string const&, std::string
const&)' 4 2362
sam wrote:
.... 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);
This is defined as a regular member function. virtual ~Parser() {};
protected: vector<string> v_data; };
.... inline string trim (const string & s, const string & t)
This is defined as a global function. { string d (s); return trim_left (trim_right (d, t), t) ; } // end of trim
Did you mean:
inline string Parser::trim (const string & s, const string & t)
?
Since these functions do not access the object in any way, you can
declare them static member functions. (and probably should).
inline Parser::Parser(string &cmd) { const char *s = cmd.c_str(); string str = trim(s);
This accesses the member function. 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::Parser(std::string&)': : undefined reference to `Parser::trim(std::string const&, std::string const&)'
Not the linker is looking for Parser::trim. You have defined ::trim. class Parser {
..... // trim spaces from both sides string trim (const string & s, const string & t = SPACES);
..... };
trim is declarated as a member function of Parser
inline string trim (const string & s, const string & t) { string d (s); return trim_left (trim_right (d, t), t) ; } // end of trim
Here it is defined as a global function
inline Parser::Parser(string &cmd) {
..... string str = trim(s);
...... }
Here the member function trim is called, not the global function.
`Parser::Parser(std::string&)': : undefined reference to `Parser::trim(std::string const&, std::string const&)'
This is because Parser::trim is declared but not defined.
My guess is that you forgot to qualify the member function name in the definition
as below: inline string Parser::trim (const string & s, const string & t) { string d (s); return trim_left (trim_right (d, t), t) ; } // end of trim
Alternatively you may remove the declaration in Parser since trim does not
need to be a Parser member.
---
Thierry Miceli www.ideat-solutions.com
Thierry Miceli wrote: class Parser { ....
// trim spaces from both sides string trim (const string & s, const string & t = SPACES);
....
};
trim is declarated as a member function of Parser
inline string trim (const string & s, const string & t) { string d (s); return trim_left (trim_right (d, t), t) ; } // end of trim
Here it is defined as a global function
inline Parser::Parser(string &cmd) {
....
string str = trim(s);
.....
}
Here the member function trim is called, not the global function.
`Parser::Parser(std::string&)': : undefined reference to `Parser::trim(std::string const&, std::string const&)'
This is because Parser::trim is declared but not defined.
My guess is that you forgot to qualify the member function name in the definition as below:
inline string Parser::trim (const string & s, const string & t) { string d (s); return trim_left (trim_right (d, t), t) ; } // end of trim
Alternatively you may remove the declaration in Parser since trim does not need to be a Parser member.
Thanks for the help.
In regarding to moving the trim() stuff to another class, I decided to
create a seperate static class to hold this sort of functions.
Do you think the following static class declaration is fine?
But I m not sure whether a static constructor/destructor is neccessary.
class TextUtil
{
public:
// trim spaces from right (you can define what spaces are)
static string trim_right (const string & s, const string & t =
SPACES);
// trim spaces from left
static string trim_left (const string & s, const string & t =
SPACES);
// trim spaces from both sides
static string trim (const string & s, const string & t = SPACES);
private:
//TextUtil() {};
};
Thanks
Sam
--- Thierry Miceli www.ideat-solutions.com
sam wrote: class TextUtil { public: static string trim_right (const string & s, const string & t = SPACES); static string trim_left (const string & s, const string & t = SPACES); static string trim (const string & s, const string & t = SPACES);
private: //TextUtil() {}; };
It looks like you would be better off with a namespace:
namespace TextUtil
{
string trim_right(const string &s,
const string &t = SPACES);
};
I don't see anything gained by the class-with-static-members
approach over the namespaces approach. But the namespaces
approach gives you the ability to import symbols with 'using'. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: A |
last post by:
Hi,
I'm having problems completing a project in C++. I have been using inline
functions in some of my header files. I have only done so for simple
functions that only have 1 statement (eg. accessor and mutator methods to
access private data members). I would like to know if there are any issues
with using inline functions that may have...
|
by: Chris Mantoulidis |
last post by:
I am not clear with the use of the keyword inline... I believe you add
it do a function when you implement the function inside the header
file where the class is stored...
But is that all? What am I missing?
If that's all, then why did Bjarne even bother adding it to the
language?
If that's not all, what else can I do with "inline"?
|
by: Richard Hayden |
last post by:
Hi,
I have the following code:
/******************************** file1.c
#include <iostream>
extern void dummy();
inline int testfunc() {
|
by: Grumble |
last post by:
Hello everyone,
As far as I understand, the 'inline' keyword is a hint for the compiler
to consider the function in question as a candidate for inlining, yes?
What happens when a function with extern linkage is inlined? Should the
compiler still export the function?
Or is an inlined function implicitly static?
|
by: Tony Johansson |
last post by:
Hello experts!
I reading a book called programming with design pattern revealed
by Tomasz Muldner and here I read something that sound strange.
Here is the whole section:
It says" Because inline functions are expanded at compile time, definitions
of these
functions, unlike other definitions cannot be separately compiled and must
be
| |
by: RainBow |
last post by:
Greetings!!
I introduced the so-called "thin-template" pattern for controlling the
code bloat caused due to template usage.
However, one of the functions in the template happens to be virtual as
well. To support thin-template, I need to make virtual function as
inline.
Now, I know that compiler would generate an out-of-line copy when it
|
by: Bilgehan.Balban |
last post by:
Hi,
If I define an inline function in one .c file, and use it from another,
after compiling and linking the two, it seems the function is not
inlined but rather called as a regular function. I would expect to see
it inlined during linkage of the two object files. Does inlining only
occur if the inline function is defined within the same...
|
by: Wu Shaohua |
last post by:
Hi Guys,
1. As we know usually we should not define a constructor as inline. I also
learned if we define a member function inside the class this member function
will be automatically be inline'ed. My question is:
If I define a constructor (including its body) or another large member
function inside the class, the constructor or the...
|
by: sam_cit |
last post by:
Hi Everyone,
I have few questions on inline functions, when i declare a function as
inline, is it for sure that the compiler would replace the function
call with the actual body of the function? or is it a call taken by
compiler?
Second, i see that it is same as what Macro's used to do for c, if so
what is the advantage for going in for...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |