M
magnus.moraberg
Hi,
When I create a class, I normally give it an exception Class -
class MyClass
{
private:
MyClassExpection(): public runtime_error
{
public:
MyClassExpection(const string& what) :
runtime_error("Exception within MyClass: " + what)
{
}
};
};
I can then use this class as follows within MyClass scope -
stringstream failStringStream;
failStringStream << "The Step Id: " << (unsigned int)step << " is
invalid";
throw MyClassExpection(failStringStream.str());
But I was wondering if I could add another constructor to my exception
class as follows -
MyClassExpection(const stringstream& what) :
runtime_error("Exception within MyClass: " + stringstream.str
())
{
}
and then use it like this -
throw MyClassExpection( stringstream() << "The Step Id: " <<
(unsigned int)step << " is invalid" );
or the less clear -
throw MyClassExpection( stringstream("The Step Id: ") << (unsigned
int)step << " is invalid" );
but these give a compiler error -
no matching function for call to
‘FS::MyClass::MyClassExpection::MyClassExpection
(std::basic_ostream<char, std::char_traits<char> >&)’
This does work -
stringstream failStringStream;
failStringStream << "The Step Id: " << (unsigned int)step << " is
invalid";
throw MyClassExpection(failStringStream);
why do I get this error?
are there any better solutions to the issue I'm trying to solve. That
is, the creation of a convenient exception object on one line.
Thanks,
Barry.
When I create a class, I normally give it an exception Class -
class MyClass
{
private:
MyClassExpection(): public runtime_error
{
public:
MyClassExpection(const string& what) :
runtime_error("Exception within MyClass: " + what)
{
}
};
};
I can then use this class as follows within MyClass scope -
stringstream failStringStream;
failStringStream << "The Step Id: " << (unsigned int)step << " is
invalid";
throw MyClassExpection(failStringStream.str());
But I was wondering if I could add another constructor to my exception
class as follows -
MyClassExpection(const stringstream& what) :
runtime_error("Exception within MyClass: " + stringstream.str
())
{
}
and then use it like this -
throw MyClassExpection( stringstream() << "The Step Id: " <<
(unsigned int)step << " is invalid" );
or the less clear -
throw MyClassExpection( stringstream("The Step Id: ") << (unsigned
int)step << " is invalid" );
but these give a compiler error -
no matching function for call to
‘FS::MyClass::MyClassExpection::MyClassExpection
(std::basic_ostream<char, std::char_traits<char> >&)’
This does work -
stringstream failStringStream;
failStringStream << "The Step Id: " << (unsigned int)step << " is
invalid";
throw MyClassExpection(failStringStream);
why do I get this error?
are there any better solutions to the issue I'm trying to solve. That
is, the creation of a convenient exception object on one line.
Thanks,
Barry.