Jump to content

Problem with templates

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
merfin

merfin

    Newbie

  • Members
  • Pip
  • 2 posts
Hi,

I have begun programming matrix class using templates. The definition of class is as follows:
template <class T> class matrix
Now I'm trying to program a class for exception handling. It would take the matrix class where error happened and a message as parameters and could be used as:
throw new matrixError ( this, "An error");

But as the matrix class uses templates, I am not able to just define constructor like
matrixError ( matrix * m, std::string msg );

I think that something like
template <class T> class matrixError

throw new matrixError<T>(this, "An error");
would solve the problem, but I first ask is there some other, and better ways to solve this problem, or am I developing this whole exception handling from totally wrong view?

#2
v0id

v0id

    Retired

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,936 posts
As far as I know there's no other ways than doing like you show in the end of your post. I would be glad to hear, if somebody else has a solution to this, though.

#3
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
If I remember right (my resources are at home) there are also template functions that will do what you want.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#4
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,720 posts
You need to create MatrixError as a template class as well, and then have the template type passed in with the constructor:


throw new MatrixError<T>(this,message);



#5
v0id

v0id

    Retired

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,936 posts
That's exactly what he figured out himself, but he's asking if there's another way to do it, without making the exception-class a template-class too.

#6
merfin

merfin

    Newbie

  • Members
  • Pip
  • 2 posts
Thanks for replies. I tried to use templates with matrixError class too, but met the same problem again: How can I tell compiler that I don't care about the datatype of template? Now I am not able to define the catch-block to catch any type of matrixErrors.

#7
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,720 posts
Create a superclass to MatrixError (e.g. Error), even if it doesn't do anything. Then in your try-catch blocks, just use the name of the superclass instead. Since MatrixError IS_A Error, it should work fine. I emphasize "should".