Hi all,
I am trying to implement a class to store data, which should be able to read its data from a string (or stringstream).
I am trying something like
1 2 3
|
Type value; // Type is the template type
wstringstream wss = ... // holds the substring with the data
wss >> value;
| |
For numerical types, this works well. However, when Type is a string type, ">>" stops after the first whitespace. I want to have all the data from wss; for the string types, this would just be a copy. How can I achieve this, template-based?
I tried this, where I put the conversion in a separate function, to create specializations for string data types (only wstring shown here). This compiles fine, but my linker (VS2005) claims that there are duplicate symbols (error LNK2005). Obviously it can't distinguish between the two _convert functions.
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
|
// Laden aus XML
template <typename Typ> void Parameter<Typ>::_convert(const wstring& data, Typ& value)
{
wstringstream result(data);
result >> value;
}
void Parameter<wstring>::_convert(const wstring& data, wstring& value)
{
value = data;
}
template <typename Typ> void Parameter<Typ>::XMLLaden(const wstring xml)
{
// Array aller möglichen XML-Tags bauen
VectorWString xml_tags = GetXMLZusatztags();
xml_tags.push_back(GetKurzname());
for (VectorWString::iterator tag = xml_tags.begin(); tag != xml_tags.end(); tag++)
{
// Regex zusammenbauen
wstring str = L"<" + *tag + L">(.*?)</" + *tag + L">";
boost::wregex expression(str, boost::regex::icase);
boost::match_results<wstring::const_iterator> match;
if (boost::regex_search(xml, match, expression))
{
Typ w;
_convert(wstring(match[1].first, match[1].second), w);
SetWert(w);
}
}
}
| |
Sorry for the mixed language in the source code. I hope you get the idea.
Any ideas are highly appreciated!
Best regards
Matthias