473,397 Members | 2,056 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Query regd. validity of a C++ program

Hi,

The other day, I was experimenting with Predicates and STL and came
across a gotcha with the following program. The objective of the
program is to remove those strings, from a vector of strings, whose
size is of atleast a specified length (here 5) and contains atleast a
specified number of punctuation characters, excluding '@' (here 2).
There are 3 classes: Length and NumberOfSpecialChars in 'Parameters'
namespace (which are just wrappers around 'unsigned long') and
SpecialStringChecker in 'Processor' namespace. The problem, I am
facing, is the following program isn't getting complied under both
GNU's 'g++' and SUN Studio's 'CC'. Both are trying to convert
std::basic_string<...to `Parameters::Length' (Don't know, why?) in
the remove_if(...) call. Interestingly, the program gets complied and
runs as expected if the commented lines are substituted for the
following uncommented parts. I am not sure, why are the compliers
reporting a problem with user-defined types (Length and
NumberOfSpecialChars), but not for a fundamental type (unsigned long)
or am I blowing this somewhere?

#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <string>
#include <iterator>
#include <ctype.h>

namespace Parameters{

class Length{
unsigned long length_;
public:
explicit Length(const unsigned long& length): length_(length) {}
operator unsigned long() const{
return length_;
}
};

class NumberOfSpecialChars{
unsigned long numberOfSpecialChars_;
public:
explicit NumberOfSpecialChars(const unsigned long&
numberOfSpecialChars):
numberOfSpecialChars_(numberOfSpecialChars) {}
operator unsigned long() const{
return numberOfSpecialChars_;
}
};

}

namespace Processor{

using namespace Parameters;
using namespace std;

class SpecialStringChecker{
Length length_;
NumberOfSpecialChars numberOfSpecialChars_;
public:
bool operator()(const string& s) const{
if(s.length() >= (unsigned long)(length_))
{
unsigned long matches = count_if(s.begin(), s.end(), *this);

if(matches >= (unsigned long)(numberOfSpecialChars_))
return true;
}
return false;
}
bool operator()(const char& c) const{
if(ispunct(c) && c !='@')
return true;
return false;
}
/*explicit SpecialStringChecker(const unsigned long& length,
const unsigned long& numberOfSpecialChars):
length_(length),
numberOfSpecialChars_(numberOfSpecialChars) {}*/
explicit SpecialStringChecker(const Length& length,
const NumberOfSpecialChars&
numberOfSpecialChars):
length_(length),
numberOfSpecialChars_(numberOfSpecialChars) {}
};

}

using namespace std;
using namespace Parameters;
using namespace Processor;

int main(int argc, char* argv[]){
vector<stringmyStrVec;
myStrVec.push_back(string("%@text1&!#"));
myStrVec.push_back(string("text2"));
myStrVec.push_back(string("@text3"));
myStrVec.push_back(string("!#$%"));
unsigned long length=5, numberOfSpecialChars=2;
//SpecialStringChecker myStrChkr(length, numberOfSpecialChars);
SpecialStringChecker myStrChkr(Length(length),
NumberOfSpecialChars(numberOfSpecialChars));
vector<string>::iterator myStrVecItr;
myStrVecItr = remove_if(myStrVec.begin(), myStrVec.end(), myStrChkr);
myStrVec.erase(myStrVecItr, myStrVec.end());
copy(myStrVec.begin(), myStrVec.end(),
ostream_iterator<string>(cout,"\n"));
return 0;
}

//Thanks in advance to the takers!!

Sep 25 '06 #1
2 1540
frame wrote:
Hi,

The other day, I was experimenting with Predicates and STL and came
across a gotcha with the following program. The objective of the
program is to remove those strings, from a vector of strings, whose
size is of atleast a specified length (here 5) and contains atleast a
specified number of punctuation characters, excluding '@' (here 2).
There are 3 classes: Length and NumberOfSpecialChars in 'Parameters'
namespace (which are just wrappers around 'unsigned long') and
SpecialStringChecker in 'Processor' namespace. The problem, I am
facing, is the following program isn't getting complied under both
GNU's 'g++' and SUN Studio's 'CC'. Both are trying to convert
std::basic_string<...to `Parameters::Length' (Don't know, why?) in
the remove_if(...) call. Interestingly, the program gets complied and
runs as expected if the commented lines are substituted for the
following uncommented parts. I am not sure, why are the compliers
reporting a problem with user-defined types (Length and
NumberOfSpecialChars), but not for a fundamental type (unsigned long)
or am I blowing this somewhere?

#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <string>
#include <iterator>
#include <ctype.h>

namespace Parameters{

class Length{
unsigned long length_;
public:
explicit Length(const unsigned long& length): length_(length) {}
operator unsigned long() const{
return length_;
}
};

class NumberOfSpecialChars{
unsigned long numberOfSpecialChars_;
public:
explicit NumberOfSpecialChars(const unsigned long&
numberOfSpecialChars):
numberOfSpecialChars_(numberOfSpecialChars) {}
operator unsigned long() const{
return numberOfSpecialChars_;
}
};

}

namespace Processor{

using namespace Parameters;
using namespace std;

class SpecialStringChecker{
Length length_;
NumberOfSpecialChars numberOfSpecialChars_;
public:
bool operator()(const string& s) const{
if(s.length() >= (unsigned long)(length_))
{
unsigned long matches = count_if(s.begin(), s.end(), *this);

if(matches >= (unsigned long)(numberOfSpecialChars_))
return true;
}
return false;
}
bool operator()(const char& c) const{
if(ispunct(c) && c !='@')
return true;
return false;
}
/*explicit SpecialStringChecker(const unsigned long& length,
const unsigned long& numberOfSpecialChars):
length_(length),
numberOfSpecialChars_(numberOfSpecialChars) {}*/
explicit SpecialStringChecker(const Length& length,
const NumberOfSpecialChars&
numberOfSpecialChars):
length_(length),
numberOfSpecialChars_(numberOfSpecialChars) {}
};

}

using namespace std;
using namespace Parameters;
using namespace Processor;

int main(int argc, char* argv[]){
vector<stringmyStrVec;
myStrVec.push_back(string("%@text1&!#"));
myStrVec.push_back(string("text2"));
myStrVec.push_back(string("@text3"));
myStrVec.push_back(string("!#$%"));
unsigned long length=5, numberOfSpecialChars=2;
//SpecialStringChecker myStrChkr(length, numberOfSpecialChars);
SpecialStringChecker
myStrChkr(Length(length),
NumberOfSpecialChars(numberOfSpecialChars));
The above appears to be parsed as a function declaration. Make that

SpecialStringChecker myStrChkr((Length(length)),
NumberOfSpecialChars(numberOfSpecialChars));

Note the additional pair of parentheses.
vector<string>::iterator myStrVecItr;
myStrVecItr = remove_if(myStrVec.begin(), myStrVec.end(), myStrChkr);
myStrVec.erase(myStrVecItr, myStrVec.end());
copy(myStrVec.begin(), myStrVec.end(),
ostream_iterator<string>(cout,"\n"));
return 0;
}
Best

Kai-Uwe Bux
Sep 25 '06 #2

Kai-Uwe Bux wrote:
frame wrote:
Hi,

The other day, I was experimenting with Predicates and STL and came
across a gotcha with the following program. The objective of the
program is to remove those strings, from a vector of strings, whose
size is of atleast a specified length (here 5) and contains atleast a
specified number of punctuation characters, excluding '@' (here 2).
There are 3 classes: Length and NumberOfSpecialChars in 'Parameters'
namespace (which are just wrappers around 'unsigned long') and
SpecialStringChecker in 'Processor' namespace. The problem, I am
facing, is the following program isn't getting complied under both
GNU's 'g++' and SUN Studio's 'CC'. Both are trying to convert
std::basic_string<...to `Parameters::Length' (Don't know, why?) in
the remove_if(...) call. Interestingly, the program gets complied and
runs as expected if the commented lines are substituted for the
following uncommented parts. I am not sure, why are the compliers
reporting a problem with user-defined types (Length and
NumberOfSpecialChars), but not for a fundamental type (unsigned long)
or am I blowing this somewhere?

#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <string>
#include <iterator>
#include <ctype.h>

namespace Parameters{

class Length{
unsigned long length_;
public:
explicit Length(const unsigned long& length): length_(length) {}
operator unsigned long() const{
return length_;
}
};

class NumberOfSpecialChars{
unsigned long numberOfSpecialChars_;
public:
explicit NumberOfSpecialChars(const unsigned long&
numberOfSpecialChars):
numberOfSpecialChars_(numberOfSpecialChars) {}
operator unsigned long() const{
return numberOfSpecialChars_;
}
};

}

namespace Processor{

using namespace Parameters;
using namespace std;

class SpecialStringChecker{
Length length_;
NumberOfSpecialChars numberOfSpecialChars_;
public:
bool operator()(const string& s) const{
if(s.length() >= (unsigned long)(length_))
{
unsigned long matches = count_if(s.begin(), s.end(), *this);

if(matches >= (unsigned long)(numberOfSpecialChars_))
return true;
}
return false;
}
bool operator()(const char& c) const{
if(ispunct(c) && c !='@')
return true;
return false;
}
/*explicit SpecialStringChecker(const unsigned long& length,
const unsigned long& numberOfSpecialChars):
length_(length),
numberOfSpecialChars_(numberOfSpecialChars) {}*/
explicit SpecialStringChecker(const Length& length,
const NumberOfSpecialChars&
numberOfSpecialChars):
length_(length),
numberOfSpecialChars_(numberOfSpecialChars) {}
};

}

using namespace std;
using namespace Parameters;
using namespace Processor;

int main(int argc, char* argv[]){
vector<stringmyStrVec;
myStrVec.push_back(string("%@text1&!#"));
myStrVec.push_back(string("text2"));
myStrVec.push_back(string("@text3"));
myStrVec.push_back(string("!#$%"));
unsigned long length=5, numberOfSpecialChars=2;
//SpecialStringChecker myStrChkr(length, numberOfSpecialChars);
SpecialStringChecker
myStrChkr(Length(length),
NumberOfSpecialChars(numberOfSpecialChars));

The above appears to be parsed as a function declaration. Make that

SpecialStringChecker myStrChkr((Length(length)),
NumberOfSpecialChars(numberOfSpecialChars));

Note the additional pair of parentheses.
vector<string>::iterator myStrVecItr;
myStrVecItr = remove_if(myStrVec.begin(), myStrVec.end(), myStrChkr);
myStrVec.erase(myStrVecItr, myStrVec.end());
copy(myStrVec.begin(), myStrVec.end(),
ostream_iterator<string>(cout,"\n"));
return 0;
}

Best

Kai-Uwe Bux
Hey, Thanks a lot!

Sep 25 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Raja | last post by:
Dear Members, Has anyone used the Vector Draw (www.vdraw.com) Component in Visual Basic. I need to create an application to be able to draw the plan view of a building. Any help in this regard...
1
by: Nick | last post by:
Deear All, I would be thankful if you could help me with the following query. I have two tables TableA and TableB TableA Name: David Address: 123 West Side City: New York
16
by: jacob navia | last post by:
Valid pointers have two states. Either empty (NULL), or filled with an address that must be at a valid address. Valid addresses are: 1) The current global context. The first byte of the data...
7
by: MLH | last post by:
?dcount("","qryOwnrsDueITSwMissingAddr") when run in the immediate window return a number greater than the number of rows that display when the saved query is run - opening in the database window?...
3
by: muppadivya | last post by:
Hi, I want to write an avi to flv converter in php but i am a complete newbie to it. I would like to know the process of how a converter code is written (ie. the basic algorithm ) . Any good...
9
by: Andy Dingley | last post by:
Here's a chunk of a longer piece of punditry I'm working on, re: the choices between doctypes for authoring and the State of the Union for validity. I've got a bucketload of nasty code and a bunch...
1
by: frame | last post by:
Hi, I am trying to compile the following program, whose fragments are presented in Section 2.1: "Compile-Time Assertions" of Chapter 2: "Techniques" of "Modern C++ Design" by Andrei...
9
by: xhe | last post by:
Hi, I need to program to check the validity of IP address through PHP Initially I used this one: $url="http://www.ntc.gov.au/ViewPage.aspx? page=A02400304500100020"; $fp=fopen($url,"r");...
4
by: istillshine | last post by:
I have a function foo, shown below. Is it a good idea to test each argument against my assumption? I think it is safer. However, I notice that people usually don't test the validity of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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...
0
tracyyun
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...
0
isladogs
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.