Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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(const std::string& msg) :
            std::runtime_error(msg) { }
};
like image 551
vimalloc Avatar asked Mar 21 '26 04:03

vimalloc


1 Answers

Move the declaration of Invalid_State_Exception above Foo.

like image 94
Daniel Gallagher Avatar answered Mar 22 '26 22:03

Daniel Gallagher



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!