Sunday, August 2, 2009

I have a problem with this program in C++ on Stacks?I get Error: Directive:Must Use C++ for Type IOSTREAM.I'

#include%26lt;iostream.h%26gt;


class ArrStack


{


int Top_of_stack;


char *Stack;


int size;


public:


ArrStack(int s=10);


~ArrStack(){delete[] Stack;}


void Push(char ch);


void Pop(void);


void Top_nopop(void);


void Top_Pop(void);


void gettop(void);


void Empty_S(void);


int Is_Empty()


{return Top_of_Stack==-1;}


int Is_Full()


{return Top_of_Stack==size-1;}


};


ArrStack::ArrStack(int s)


{size=s;


Stack=new char[size];


Top_of_stack=-1;


}


void ArrStack::Push(char ch)


{


if(Is_Full())


{cout%26lt;%26lt;"Sorry!Stack Full"%26lt;%26lt;endl;


}


else


{


Stack[++Top_of_stack]=ch;}


}


void ArrStack::Pop()


{


if(Is_Empty())


{cout%26lt;%26lt;"Sorry!Stack Empty"%26lt;%26lt;endl;


}


else{


--Top_of_Stack;}


}


void ArrStack::Top_nopop()


{


if(Is_Empty())


{cout%26lt;%26lt;"Sorry!Stack Empty"%26lt;%26lt;endl;}


else{


cout%26lt;%26lt;Stack[Top_of_stack];}


}}


void ArrStack::Top_Pop()


{


if(Is_Empty())


{cout%26lt;%26lt;"Sorry!Stack Empty"%26lt;%26lt;endl;}


else


{


cout%26lt;%26lt;Stack[Top_of_stack];


--Top_of_stack;}


}


void ArrStack::gettop()


{


cout%26lt;%26lt;Top_of_stack;


}


void Empty_S()





{


Top_of_stack=-1;


}

I have a problem with this program in C++ on Stacks?I get Error: Directive:Must Use C++ for Type IOSTREAM.I'
Your problem is that your compiler thinks this is a C file. Either your compiler or your IDE. Without knowing what compiler you are using (though I'd guess Turbo C++ or Borland C++) I'd say first, make sure the file name ends in .cpp, not .c. If it ends in .c then you are going to get the same error. Don't change the name, though, to be safe copy it into a cpp file you should then try to compile. If that doesn't work follow the instructions for compiling it from the command line (tcc %26lt;filename%26gt;.cpp or whatever).





If that doesn't work we need to know what compiler you're using specifically.





Okay, given the new information, I've cleaned this up a little and added the new syntax (since the heyday of Turbo C the header files have changed and I'm using GCC on Linux. This compiles without too many problems. The biggest change was putting ArrStack:: in front of Empty_S(). There are a few syntax errors here and there to run:





#include%26lt;iostream%26gt;





using namespace std;





class ArrStack


{


int Top_of_stack;


char *Stack;


int size;


public:


ArrStack(int s=10);


~ArrStack(){delete[] Stack;}


void Push(char ch);


void Pop(void);


void Top_nopop(void);


void Top_Pop(void);


void gettop(void);


void Empty_S(void);


int Is_Empty()


{return Top_of_stack==-1;}


int Is_Full()


{return Top_of_stack==size-1;}


};


ArrStack::ArrStack(int s)


{size=s;


Stack=new char[size];


Top_of_stack=-1;


}


void ArrStack::Push(char ch)


{


if(Is_Full())


{cout%26lt;%26lt;"Sorry!Stack Full"%26lt;%26lt;endl;


}


else


{


Stack[++Top_of_stack]=ch;}


}


void ArrStack::Pop()


{


if(Is_Empty())


{cout%26lt;%26lt;"Sorry!Stack Empty"%26lt;%26lt;endl;


}


else{


--Top_of_stack;}


}


void ArrStack::Top_nopop()


{


if(Is_Empty())


{cout%26lt;%26lt;"Sorry!Stack Empty"%26lt;%26lt;endl;}


else{


cout%26lt;%26lt;Stack[Top_of_stack];}


}


void ArrStack::Top_Pop()


{


if(Is_Empty())


{cout%26lt;%26lt;"Sorry!Stack Empty"%26lt;%26lt;endl;}


else


{


cout%26lt;%26lt;Stack[Top_of_stack];


--Top_of_stack;}


}


void ArrStack::gettop()


{


cout%26lt;%26lt;Top_of_stack;


}


void ArrStack::Empty_S()





{


Top_of_stack=-1;


}





Header files tend to begin:





#ifndef %26lt;Token of the header file%26gt;


#define %26lt;Token of the header file%26gt;





Then at the very end of the file is:


#endif





That is so if it is called twice the compiler doesn't get confused and compile it twice. In C it would return an error though with overloading I don't know what C++ would do. If this header file is called by a CPP program try saving it as an hpp. If it's not called by a CPP program (which itself includes iostream.h) then you have a problem right there. Make sure it's #included into a C++ program.


No comments:

Post a Comment