Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Error: use of undeclared identifier: exception in template class
Forum Updated to NodeBB v4.3 + New Features

Error: use of undeclared identifier: exception in template class

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 988 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    Infinity
    wrote on 13 Apr 2020, 07:15 last edited by
    #1

    I would like to create this template class:

    #include <sstream>
    #include <string>
    #include <exception>
    
    template<class T>
    std::string ToStr(const T &value)
    {
      std::ostringstream oss;
      if (!(oss << std::dec << value)) throw exception("Invalid argument");
      return oss.str();
    }
    

    But I get the following compiler error:

    error: use of undeclared identifier 'exception'
    .../conversions.h:41: Fehler: there are no arguments to ‘exception’ that depend on a template parameter, so a declaration of ‘exception’ must be available [-fpermissive]
       41 |   if(!(iss>>std::dec>>result))throw exception("Invalid argument");
          |                                     ^~~~~~~~~
    

    How do I have to handle that?

    1 Reply Last reply
    0
    • C Online
      C Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 13 Apr 2020, 07:42 last edited by
      #2

      @Infinity said in Error: use of undeclared identifier: exception in template class:

      How do I have to handle that?

      There is no class 'exception' somewhere. It's maybe std::exception what you're looking for.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      2
      • I Offline
        I Offline
        Infinity
        wrote on 13 Apr 2020, 07:58 last edited by Infinity
        #3

        I found the solution:

        #include <sstream>
        #include <string>
        #include <exception>
        
        class MyException : public std::exception
        {
            virtual const char* what() const throw() {
                return "Invalid argument";
            }
        };
        
        MyException myException;
        
        //---------------------------------------------------------------------------
        // Convert a number to a string
        
        template<class T>
        std::string NumberToStr(const T &value)
        {
            std::ostringstream oss;
            if (!(oss << std::dec << value)) throw myException;
            return oss.str();
        }
        
        1 Reply Last reply
        0

        1/3

        13 Apr 2020, 07:15

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved