illegal structure operation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ketanharsoda
    New Member
    • Mar 2015
    • 1

    illegal structure operation

    Code:
    #include<iostream.h>
    #include<conio.h>
    class date
    {
    	int dd,yy,day,*incr;
    	char *mon; //first latter
    
    	public:
    	void setptr(date *m);
    	void getdata();
    	void putdata();
    };
    void date::setptr(date *m)
    {
    	char *month[]={"jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"};
    	for(int i=0;i<=11;i++)
    	{
    	      if(*m==*month[i])
    	      {
    		*incr=*month[i];
    		cout<<*incr;
    		break;
    	      }
    	}
    }
    void date::getdata()
    {
    	date s;
    	cout<<"enter in dd,mon,yy foramt";
    	cout<<"\nenter day:";
    	cin>>dd;
    	cout<<"enter month";
    	cin>>mon;
    	cout<<"enter year";
    	cin>>yy;
    	cout<<"enter no.of days you want to enter:";
    	cin>>day;
    	s.setptr(mon);
    }
    void date::putdata()
    {
    	cout<<endl;
    	cout<<"DATE:"<<dd<<":"<<mon<<":"<<yy;
    }
    
    void main()
    {
    	clrscr();
    	date d1;
    	d1.getdata();
    	getch();
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    This code:
    Code:
    void date::setptr(date *m)
    {
    	char *month[]={"jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"};
    	for(int i=0;i<=11;i++)
    	{
    	      if(*m==*month[i])
    	      {
    		*incr=*month[i];
    		cout<<*incr;
    		break;
    	      }
    	}
    }
    has several problems.
    1) The function attempts to take an address from a local variable month[] and use it outside the function.

    2) Coding (*m==*month[i])requires and operator==(char *) member function and there is no such function.

    3) This member function is used in date::getdata by passing in a char* when the function requires a date*.

    4) The code s.setptr(mon); inside date::getdata() uses a local date variable which is then destroyed when the function completes. This may mean you need to learn how to use the this operator.

    5) main() returns an int not a void.

    6) iostream.h is pre-1998 C++. I would try to get something newer. iostream.h is not ANSI standard and has some bugs in it.

    Comment

    Working...