Thread: catch block giving parse error

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    20

    catch block giving parse error

    I can not seem to get my try/catch block to work.

    I am trying to use it in a templated class and I keep getting parse errors.

    Is my syntax correct?

    I have also tried
    Code:
    template<class T>
    catch(Test<T>::whoops)  // <--- This is giving me a parse error
    { cout<< " it works " << endl << endl;
        system("pause");
    }
    Which also gives me parse errors.

    Any pointers would be appreciated.
    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    
    using namespace std;
    template<class T>
    class Test
    { private:
        T balance;
      public:
        class whoops {};
       void withdraw(float temp);
       void see();
       void SetBalance();
    };
    
    int main()
    { Test<float> data[10];
      float try_;
      int exit = 0;
      char select = '\0';
    
      data[0].SetBalance();
    
      do
       { cout<< "(1/2) ->: ";
         cin>> select;
    
           switch(select)
            { case '1':
                 cout<< "hmm: ";
                 cin>> try_;
    
                 try
                  { data[0].withdraw(try_);
                  }
    
                  catch(Test::whoops)  // <--- This is giving me a parse error
                    { cout<< " it works " << endl << endl;
                      system("pause");
                    }
                  break;
    
             case '2':
                     data[0].see();
                break;
              default:
              cout<< "try again" << endl << endl;
               break;
          }
       }while(exit == 0);
          return 0;
    }
    template<class T>
    void Test<T>::withdraw(float temp)
    {   cout<< balance << endl;
    
      if(temp>balance)
        throw whoops();
        balance-=temp;
    }
    template<class T>
    void TestTT>::SetBalance()
    { balance = 100;
    }
    
    template<class T>
    void Test<T>::see()
    { cout<< balance;
    
    }
    Thank you,

    Rich

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Nowhere in your code have you defined a class called Test, so Test::whoops doesn't exist.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    20
    I thought that I defined it here. If I untemplate the class it works.

    Code:
     
    using namespace std;
    template<class T>
    class Test   // <<-- I thoght that I declared the class "TEST" here
    { private:
        T balance;
      public:
        class whoops {};
       void withdraw(float temp);
       void see();
       void SetBalance();
    };
    Last edited by Mr_roboto; 03-01-2006 at 01:23 PM.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    class Test // <<-- I thoght that I declared the class "TEST" here
    No, you only have a recipe for making classes called:
    template<class T>
    class Test
    After you use that recipe to make a class, you will have a class called "Test for floats" or "Test for ints", etc., but you will never have a class called just "Test". For there to be a class called just "Test", you would have to have this declaration in your code:
    Code:
    class Test
    {
    
    };
    That is not the same as:
    Code:
    template<class T>
    class Test 
    {
    
    };
    Putting that template in your program does not create a class. It's only after you issue the commands to the compiler to use that recipe to create a class that there will be an actual class in your code. Thereafter, you have to refer to the class by its correct name, e.g. "Test for floats".
    Last edited by 7stud; 03-01-2006 at 02:16 PM.

  5. #5
    Registered User
    Join Date
    Oct 2005
    Location
    Hyderabad, India
    Posts
    33
    the change mentioned below should solve ur problem
    Code:
    catch(Test<float>::whoops)  // <--- This is giving me a parse error

  6. #6
    Registered User
    Join Date
    Oct 2005
    Location
    Hyderabad, India
    Posts
    33
    also change
    Code:
    template<class T>
    void TestTT>::SetBalance()
    { balance = 100;
    }
    to
    Code:
    template<class T>
    void Test<T>::SetBalance()
    { balance = 100;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  3. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  4. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  5. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM