By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,952 Members | 1,033 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,952 IT Pros & Developers. It's quick & easy.

could you please tell me whats the error in the code

P: 39
i am using vc++ compiler
Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. using namespace std;
  3. class base
  4. {
  5. public:
  6.  
  7.     virtual void paint()
  8.     {
  9.         cout<<"in middle\n";
  10.     }
  11. };
  12.  
  13. class derived : public base
  14. {
  15. public:
  16.  
  17.     void paint()
  18.     {
  19.         cout<<"in derived\n";
  20.     }
  21.  
  22. };
  23. class middle 
  24. {
  25.     char str[10];
  26. public :
  27.     derived *getinfo()
  28.     {
  29.         cout<<"which object you want\n";
  30.             cin>>str;
  31.         if (str == "derived")
  32.         {             
  33.             return new(derived);
  34.         }
  35.     }
  36. };
  37. int main()
  38. {
  39.     middle m;
  40.     base *b;
  41.     //derived *d;
  42.     derived d;
  43.     b = m.getinfo();
  44.     b->paint();
  45.     getchar();
  46.     return 0;
  47.  
  48.  
  49. }
  50.  
  51.  
Unhandled exception at 0x00000000 in project.exe: 0xC0000005: Access violation reading location 0x00000000.

this is the error i am getting
Sep 22 '07 #1
Share this Question
Share on Google+
3 Replies


Ganon11
Expert 2.5K+
P: 3,652
You can't compare strings using the == operator; you have to use strcmp.
Sep 22 '07 #2

P: 39
You can't compare strings using the == operator; you have to use strcmp.

thanx this was a stupid mistake that i have done

thanx a lot once again
Sep 22 '07 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
While we're at it, I see your are using C++ so why use C-style strings??

You should be using C++ string objects. These you can compare using the == operator.
Sep 22 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.