What should I do if ma compiler says undefined symbol try during exception handling??
undefined try symbol
Collapse
X
-
Tags: None
-
Probably you should show us the code. You could also check this old thread where someone had the same problem. It might help. -
I remember doing something like that. In my case I used a variable from inside the catch block that was defined in the try block. The try block is out of scope when the catch block starts so the variable was undefined. All I had to do was define the variable outside the try block and everything worked.Comment
-
Code:#include<iostream.h> #include<conio.h> void check(int ,int,int); class e1{}; class day { private: int date,month,year; public: void getdata() { cout<<"Enter date,month,year\n"; cin>>date>>month>>year; } void display() { cout<<date<<"/"<<month<<"/"<<year; } void get() { check(date,month,year); } }; void main() { day d; d.getdata(); try { void check(int d1,int m1,int y1) { if(d1>31||m1>12||y1>13) { throw e1; } } } catch(e1) { cout<<"Invalid"; } d.display(); getch(); }Last edited by Banfa; Sep 26 '13, 08:27 AM. Reason: Added code tags [code] ... [/code] round the codeComment
-
There are a few things wrong with that code, so let's go through them one by one:- If you want to use cin and cout without adding the namespace to every call (which would be std::cin and std::cout) you'll have to use that namespace by adding after your importsCode:
using namespace std;
- You define a function inside the try block. That is not possible in C/C++; but as you have a prototype at the top, you can add it right at the bottom of the code.
- Instead of defining the check-function in that catch block you probably want to call Code:
d.get();
- You can't throw a class but you can throw an instance of a class. So instead of you probably wantCode:
throw e1;
Code:throw new e1;
Btw, please use [CODE] tags when posting code. It makes it much easier to read.Comment
- If you want to use cin and cout without adding the namespace to every call (which would be std::cin and std::cout) you'll have to use that namespace by adding
-
-
-
Comment