stream problem

hi,
I'm doing a project in which I'm accepting a tag and content string and converting into an XML which is my extended class of the string class. However I'm running into an error with my constructor

1
2
3
4
5
6
7
8
CXMLString::CXMLString(string content, string tag)
{
	stringstream mystream;
	mystream << "<" << tag << ">" << content << "</" << tag << ">";
	
	*this = mystream.str();

}


but if i replace:
*this = mystream.str();
with
this->assign(mystream.str());
its working fine.

Could someone explain why and help me if I could do it the way I want to implement
So you have a class of type CXMLString, and you're asking the compiler to use the = operator (the assignment operator) on it with an input of mystream.str().

Has the code for that been written? This is "operator overloading" and has to be written. If it hasn't been written, it's like trying to use a function that does not exist.
Last edited on
CXMLString inherits String class and i just want to return a stream to my object so I really don't think i need to overload = operator. And even if i need to how do you suggest i make the stream equal to an object of CXMLString.
Also, I had done another project in which i did something similar like:

1
2
3
4
5
6
7
8
9
CString::CString(int number)
{
	stringstream mystream;

	mystream << number;

	*this = mystream.str();

}


and here CString was again inheriting String class and this code works just fine.
Also, I tried something else something like:
1
2
3
4
CXMLString::CXMLString(string content, string tag)
{
	*this = "<" + tag + ">" + content + "</" + tag + ">";
}

and even this works good!
Topic archived. No new replies allowed.