could you please tell me whats the error in the code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • diwakar09
    New Member
    • Sep 2007
    • 39

    could you please tell me whats the error in the code

    i am using vc++ compiler
    Code:
    #include<iostream>
    using namespace std;
    class base
    {
    public:
    	
    	virtual void paint()
    	{
    		cout<<"in middle\n";
    	}
    };
    
    class derived : public base
    {
    public:
    	
    	void paint()
    	{
    		cout<<"in derived\n";
    	}
    
    };
    class middle 
    {
    	char str[10];
    public :
    	derived *getinfo()
    	{
    		cout<<"which object you want\n";
    			cin>>str;
    		if (str == "derived")
    		{             
    			return new(derived);
    		}
    	}
    };
    int main()
    {
    	middle m;
        base *b;
    	//derived *d;
    	derived d;
    	b = m.getinfo();
    	b->paint();
    	getchar();
    	return 0;
    	
    	
    }
    Unhandled exception at 0x00000000 in project.exe: 0xC0000005: Access violation reading location 0x00000000.

    this is the error i am getting
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    You can't compare strings using the == operator; you have to use strcmp.

    Comment

    • diwakar09
      New Member
      • Sep 2007
      • 39

      #3
      Originally posted by Ganon11
      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

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        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.

        Comment

        Working...