Template overloading
Hi!
I'm trying to overload my constructor and it's showing an error saying cannot overload.
this is my header file and the code was working fine before i tried overloading the constructor
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
#ifndef CXMLString_H
#define CXMLString_H
#include <iostream>
#include <sstream>
using namespace std;
namespace CXML
{
enum XMLParseResult_t
{
XMLFULLLINE,
XMLOPENTAG,
XMLCLOSETAG,
XMLERRORSYNTAX,
XMLERRORTAGMISMATCH,
_XMLMAX
};
template<class T>
class CXMLString: public string
{
private:
bool extractTag(CXMLString& tag, CXMLString& remainder);
void trim(string const& temp_compare);
bool contains(string const sub);
static string m_error[_XMLMAX];
/** @link dependency */
/*# XMLParseResult_t lnkXMLParseResult_t; */
public:
CXMLString(bool content, string tag = "")
{
stringstream mystream;
if (content == true)
mystream << "<" << tag << ">" << "true" << "</" << tag << ">";
else
mystream << "<" << tag << ">" << "true" << "</" << tag << ">";
this->assign(mystream.str());
}
CXMLString(T content, string tag = "")
{
stringstream mystream;
if (tag.length())
mystream << "<" << tag << ">" << content << "</" << tag << ">";
else
mystream << content;
//str() is used for get/set with the associated string
string temp = mystream.str();
this->assign(mystream.str());
//*this = "<" + tag + ">" + temp + "</" + tag + ">";
}
static string error(XMLParseResult_t);
XMLParseResult_t parse(string& tag, string& content);
};
}
#endif
|
any help or suggestions :(
If you use std::boolalpha
in CXMLString(T content, string tag = "")
mystream << "<" << tag << ">" << std::boolalpha << content
the bool overload would not be required at all, would it?
wasn't aware of something as boolalpha. Tried it out and it works! Thanks.
This is not a good idea:
class CXMLString: public std::string
Either use containment or if you must inherit, inherit privately.
With public inheritance:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
#include <string>
#include <iostream>
#include <sstream>
void foo( std::string& xml_str ) { xml_str = "this is not an xml string" ; }
template<class T> class CXMLString : public std::string
{
public: CXMLString( T content, std::string tag = "" )
{
std::ostringstream stm ;
stm << "<" << tag << ">" << content << "</" << tag << ">" ;
assign( stm.str() ) ;
}
};
int main()
{
CXMLString<int> xml_str( 23, "value" ) ;
std::cout << xml_str << '\n' ;
foo( xml_str ) ;
std::cout << xml_str << '\n' ;
}
|
Topic archived. No new replies allowed.