Originally Posted by
matsp
I think the confusion about namespaces and header files come from the common statement of "do not put using namespace XXX" in a header file. Which you shouldn't.
If for some reason you want use namespaces for a particular group of functions, variables, type declaraions or constants, then you put "namespace somename" around the those things. If you have another group of things you want in another namespace, then you put another namespace around those. As many as you think makes sense. Commonly, you wouldn't put more than one namespace group in one header file, but there's nothing saying you CAN NOT do that.
--
Mats
I'm having problems linking this namespace
Code:
#ifndef VALID_H
#define VALID_H
#include <boost/regex.hpp>
#include <string>
#include <time.h>
#define YR_OLD 2
namespace valid
{
/* Validate RSA ID number */
bool driverIDValidator(const std::string& id)
{
static const boost::regex e("^[0-9]{6}[0-9]{4}[0-9]{3}$");
return regex_match(id, e);
}
// Validate RSA land line number
bool landLineValidator(const std::string& lndln)
{
static const boost::regex e("(0|\\+27)[1234][123][-| ]+"
"[0-9]{3}[-| ]+([0-9]{4})$");
return regex_match(lndln, e);
}
// Validate RSA cell/toll-free number
bool cellNoValidator(const std::string& cn)
{
static const boost::regex e("(0|\\+27)[78]([01234]|[68])[-| ]+"
"[0-9]{3}[-| ]+([0-9]{4})$");
return regex_match(cn, e);
}
// Validate string not containing numbers
bool stringValidator(const std::string& s)
{
static const boost::regex e("^[a-zA-Z ]+$");
return regex_match(s, e);
}
// Validate string containing numbers
bool mixedStringValidator(const std::string& s)
{
static const boost::regex e("^[a-zA-Z0-9 ]+$");
return regex_match(s, e);
}
// Validate RSA postal code
bool postalCodeValidator(const std::string& c)
{
static const boost::regex e("^([0-9]{4})$");
return regex_match(c, e);
}
// Validate car engine size
bool engineCapValidator(const std::string& c)
{
static const boost::regex e("^[0-9]{2}(00)$");
return regex_match(c, e);
}
// Validate RSA char/digit value
int charValidator(const char& c)
{
return isdigit(c);
}
int rentalTypeValidator(const std::string& s)
{
return !( s.compare("Unlimited") && s.compare("200km") );
}
bool yearModelValidator(const int& value)
{
time_t rawtime;
tm* timeinfo;
time( &rawtime );
timeinfo = localtime ( &rawtime );
int year = timeinfo->tm_year+1900;
if(value < year-YR_OLD && value > year)
return false;
else
return true;
}
}
#endif
Used here ...
Code:
#include "Validator.h"
void Fleet::ReadCarData(Car& car)
{
string make, model, regNo;
char engineSize[10];
int ymodel;
Car::TankStatus ts;
do
{
cout << "\n\nEnter car make : ";
cin >> make;
}
while ( !valid::mixedStringValidator(make) );
do
{
cout << "\nEnter car model : ";
cin >> model;
}
while ( !valid::mixedStringValidator(model) );
do
{
cout << "\nEnter car registration number : ";
cin >> regNo;
}
while ( !valid::mixedStringValidator(regNo) );
do
{
cout << "\nEnter the year model of the car : ";
cin >> ymodel;
}
while ( !valid::yearModelValidator(ymodel) );
do
{
cout << "\nEnter the engine capacity : ";
cin >> engineSize;
}
while ( !valid::engineCapValidator(engineSize) );
do
{
cout << "\nTank Status [unknown=0, empty=1, half=2, full=3] :";
cin >> ts;
}
while( !(ts==Car::unknown || ts==Car::empty || ts==Car::full || ts==Car::half ) );
// Ensure correct tank details
if ( (ts>3) || (ts<0) ) {
ts=Car::unknown;
}
car.setCarDetails(make, model, regNo, ymodel, atoi(engineSize), ts);
}
Link Errors
Code:
1>Rental.obj : error LNK2005: "bool __cdecl valid::driverIDValidator(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?driverIDValidator@valid@@YA_NABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) already defined in Fleet.obj
1>Rental.obj : error LNK2005: "bool __cdecl valid::landLineValidator(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?landLineValidator@valid@@YA_NABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) already defined in Fleet.obj
1>Rental.obj : error LNK2005: "bool __cdecl valid::cellNoValidator(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?cellNoValidator@valid@@YA_NABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) already defined in Fleet.obj
1>Rental.obj : error LNK2005: "bool __cdecl valid::stringValidator(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?stringValidator@valid@@YA_NABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) already defined in Fleet.obj
1>Rental.obj : error LNK2005: "bool __cdecl valid::mixedStringValidator(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?mixedStringValidator@valid@@YA_NABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) already defined in Fleet.obj
1>Rental.obj : error LNK2005: "bool __cdecl valid::postalCodeValidator(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?postalCodeValidator@valid@@YA_NABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) already defined in Fleet.obj
1>Rental.obj : error LNK2005: "bool __cdecl valid::engineCapValidator(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?engineCapValidator@valid@@YA_NABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) already defined in Fleet.obj
1>Rental.obj : error LNK2005: "int __cdecl valid::charValidator(char const &)" (?charValidator@valid@@YAHABD@Z) already defined in Fleet.obj
1>Rental.obj : error LNK2005: "int __cdecl valid::rentalTypeValidator(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?rentalTypeValidator@valid@@YAHABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) already defined in Fleet.obj
1>Rental.obj : error LNK2005: "bool __cdecl valid::yearModelValidator(int const &)" (?yearModelValidator@valid@@YA_NABH@Z) already defined in Fleet.obj
1>c:\C++ Folder\Rental Project\Fleet\Debug\Fleet.exe : fatal error LNK1169: one or more multiply defined symbols found
Rental.CPP also includes "Validator.h" ... I think the conflict is there? How do i resolved this?