c++ templated function error while trying to throw exception
I have some code where i am trying to throw a custom exception in a Templated method. When i try to compile it i get the following warning:
there are no arguments to ‘Invalid_State_Exception’ that depend on a
template parameter, so a declaration of ‘Invalid_State_Exception’ must
be available
note: (if you use ‘-fpermissive’, G++ will accept your code, but
allowing the use of an undeclared name is deprecated)
I haven't been able to figure out a way around this as of yet. Any advice would be great. Here is some sample code explaining what i have (Foo.h):
template <class T> class Foo
{
public:
void do_stuff(T t)
{
if(bar == true)
{
throw Invalid_State_Exception("FooBar error occurred");
}
}
....
};
class Invalid_State_Exception : public std::runtime_error
{
public:
Invalid_State_Exception(co开发者_运维百科nst std::string& msg) :
std::runtime_error(msg) { }
};
Move the declaration of Invalid_State_Exception
above Foo
.
You need to define Invalid_State_Exception
before throwing it in Foo::do_stuff
.
Put your Invalid_State_Exception before your template.
精彩评论