Template overloading

Hi!
Well the header file of my code I'm working on is:

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
#ifndef CXMLString_H
#define CXMLString_H

#include <iostream>
#include <sstream>
#include "CComplex.h"

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];

public:

	CXMLString(T content, string tag = "")
	{
		stringstream mystream;
		if (tag.length())
			mystream << "<" << tag << ">" << boolalpha << 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 


the code is working fine, but now I want to implement to add complex numbers to the CXMLString. I already have CComplex header that is:

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
#ifndef CCOORDINATE_H_
#define CCOORDINATE_H_

#include <iostream>
#include <cmath>

using namespace std;

template<class U>
class CComplex
{
	friend ostream& operator<< (ostream& out, const CComplex<U>& obj)
		{
			if (obj.m_imaginary > 0)
			{
				out << obj.m_real << " + i" << obj.m_imaginary;
			}
			if (obj.m_imaginary < 0)
			{	float temp = -obj.m_imaginary;
				out << obj.m_real << " - i" << temp;
			}
			if (obj.m_imaginary == 0)
			{
				out << obj.m_real;
			}
			return out;
		}

private:
	U m_imaginary;
	U m_real;

public:
	CComplex();
	CComplex(U, U);
};

template<class U>
CComplex<U>::CComplex()
{
	m_imaginary = 0;
	m_real = 0;
}

template<class U>
CComplex<U>::CComplex(U x, U y)
{
	m_imaginary = x;
	m_real = y;
}


#endif 


How do I write another constructor that would accept a complex number and make a xml string. I want to implement something like this in my main()

1
2
3
4
5
6
7
int main()
{
....
CComplex<int> a1(2,2);
CXMLString<CComplex> a1_xml(a2,"Complex");
....
}


any suggestions please? This is the first time I'm working with templates.
I think this should work in main().
1
2
	CComplex<int> a1(2,2);
	CXML::CXMLString<CComplex<int>> a1_xml(a1,"Complex");


Why would you want another constructor ? Since << operator is already overloaded for CComplex, it should be able to handle with the existing generic constructor itself.

If you still want a separate constructor for specific type, implement
 
CXMLString(CComplex content, string tag = "")

before
 
CXMLString(T content, string tag = "")
When I try

1
2
CComplex<int> a1(2,2);
CXML::CXMLString<CComplex<int>> a1_xml(a1,"Complex");


I'm gettin an error:
error: '>>' should be '> >' within a nested template argument list

how to nest templates? i'm really new at templates and if you could describe things in a little more detail that would really be very helpful!

Thanks!
Have you tried giving space between > and > as the error suggests ?
1
2
CComplex<int> a1(2,2);
CXML::CXMLString< CComplex<int> > a1_xml(a1,"Complex");


okay working now.. why were the spaces needed? was the compiler confusing itself with the stream >> operator?
Yeah, that could be the reason. My compiler(Visual studio 2012) was not giving an error.
i'm using eclipse. Also, could you help me with one problem? In the header file:
1
2
3
4
5
6
7
8
9
10
11
12
13
CXMLString(T content, string tag = "")
	{
		stringstream mystream;
		if (tag.length())
			mystream << "<" << tag << ">" << boolalpha << 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 + ">";
	}


i wanted to overload the constructor for bool passed as string and then the string true/false should be taken and not the numeric 1/0 value. I always ran into errors with another constructor:

1
2
3
4
5
6
7
8
9
CXMLString(bool content, string tag = "")
	 {
	 stringstream mystream;
	 if (content == true)
	 mystream << "<" << tag << ">" << "true" << "</" << tag << ">";
	 else
	 mystream << "<" << tag << ">" << "true" << "</" << tag << ">";
	 this->assign(mystream.str());
	 }


so finally used that alphabool keyword for a work around. But assignment specifically tells me to overload the constructor. If you tell me what possibly is wrong here. The errors I'm getting for both the constructors in this case are:

1. error: 'CXML::CXMLString<T>::CXMLString(T, std::string) [with T = bool, std::string = std::basic_string<char>]' cannot be overloaded

2. error: with 'CXML::CXMLString<T>::CXMLString(bool, std::string) [with T = bool, std::string = std::basic_string<char>]'

any suggestions pls?
Topic archived. No new replies allowed.