Hi!
I'm trying to overload my constructor and running into errors. My code is working fine with just one constructor but I try to overload it gives me errors:
The header of the working code:
Just to clarify here: ne555 means that it makes no sense to overload the constructor taking a different type, because that's pretty much nullifying the advantage of a template.
What you want to do instead is specialize the template for type bool instead.
Have a google for template specialization, there's no real mystery about it if you understand templates.
@ne555 I understand that I really don't need another constructor since all can be done with one I have, but my assignment explicitly tells me to overload the constructor so that's the way I need to do it. Also, now I'm just getting curious as to why it isn't working this way? And i'm including CComplex so that I can create xml not only with strings, ints, floats etc but even complex numbers. Again part of the excercise.
@NwN I read some stuff regarding template specialization and I'm not sure if i have been able to absorb everything since this is actually my first program ever with templates, still to what basic understanding I got I really don't think there is anything wrong with the syntax of what I'm trying to do here.
> this is actually my first program ever with templates
> my assignment explicitly tells me to overload the constructor so that's the way I need to do it.
Who gave you this assignment as a first program ever with templates? And why is such a person trying to teach templates?
An easy way to solve the problem is to take an extra constructor argument (which need not be named or used) in one of the overloaded constructors:
1 2 3 4 5
template < typename T > struct A
{
A( T, int ) { /* ... */ }
A( bool ) { /* ... */ }
};
Apparently, that is not what you are allowed to do. A correct solution then would require the use of SFINAE (which is definitely not something that is appropriate for the first template program that a student is writing).
#include <type_traits>
template < typename T > struct using_SFINAE
{
template< typename U > using_SFINAE( typename std::enable_if<
std::is_same<T,U>::value && !std::is_same<T,bool>::value, T >::type )
{ /* this overloaded constructor one is available only if T is not bool */ }
using_SFINAE( bool ) { /* overloaded constructor two - bool */ }
};
// fine, constructor one: using_SFINAE<int>::using_SFINAE(int)
using_SFINAE<int> one(25) ;
// fine, constructor two: using_SFINAE<int>::using_SFINAE(bool)
using_SFINAE<int> two(true) ;
// fine, constructor two: using_SFINAE<bool>::using_SFINAE(bool)
// constructor one is not available because T is bool
using_SFINAE<bool> three(true) ;
To clarify what I mean CXMLString<constchar*> foo(false, "tag"); makes no sense.
> now I'm just getting curious as to why it isn't working this way?
told you already. CXMLString<bool> foo; would generate the code replacing T by bool.
Now look at your class definition
class CXMLString<bool>: public string
{
public:
CXMLString(bool content, string tag = "")
{
stringstream mystream;
if (content == true)
mystream << "<" << tag << ">" << "true" << "</" << tag << ">";
else
mystream << "<" << tag << ">" << "false" << "</" << tag << ">";
assign(mystream.str());
}
//CXMLString(T content, string tag = "")
CXMLString(bool content, string tag = "")
{
stringstream mystream;
if (tag.length())
mystream << "<" << tag << ">" << boolalpha << content << "</" << tag << ">";
else
mystream << content;
string temp = mystream.str();
assign(mystream.str());
//*this = "<" + tag + ">" + temp + "</" + tag + ">";
}
¿how do you expect the compiler to differentiate the constructors?
> And i'm including CComplex so that I can create xml not only with strings, ints, floats etc but even complex numbers
Include it in main.cpp
Your class is (should be) independent of the template parameter.
> but my assignment explicitly tells me to overload the constructor so that's the way I need to do it.
bad wording, I guess.
To clarify what I mean CXMLString<constchar*> foo(false, "tag"); makes no sense.
> now I'm just getting curious as to why it isn't working this way?
told you already. CXMLString<bool> foo; would generate the code replacing T by bool.
Now look at your class definition
class CXMLString<bool>: public string
{
public:
CXMLString(bool content, string tag = "")
{
stringstream mystream;
if (content == true)
mystream << "<" << tag << ">" << "true" << "</" << tag << ">";
else
mystream << "<" << tag << ">" << "false" << "</" << tag << ">";
assign(mystream.str());
}
//CXMLString(T content, string tag = "")
CXMLString(bool content, string tag = "")
{
stringstream mystream;
if (tag.length())
mystream << "<" << tag << ">" << boolalpha << content << "</" << tag << ">";
else
mystream << content;
string temp = mystream.str();
assign(mystream.str());
//*this = "<" + tag + ">" + temp + "</" + tag + ">";
}
¿how do you expect the compiler to differentiate the constructors?
> And i'm including CComplex so that I can create xml not only with strings, ints, floats etc but even complex numbers
Include it in main.cpp
Your class is (should be) independent of the template parameter.
> but my assignment explicitly tells me to overload the constructor so that's the way I need to do it.
bad wording, I guess.