Initially , I named the variable arrsize as 'size' and it generated lots of errors which I could not understand. I will be grateful if anyone can explain it. Thanks
Moreover , the #ifndef is used to check whether the header file has been defined before .
If not defined , then it is defined and thus compiles only once #endif ends the expression.
#include<iostream>
#include<cstdlib>
#include "stack.h"
//#include "stack.cpp"
usingnamespace std ;
int main(){
Stack mystack;
int ans , item;
bool y ;
cout<<"\n\n\n Stack Operations";
while(true){
cout<<"1. Push";
cout<<"2. Pop";
cout<<"3. Peep";
cout<<"4. IsEmpty";
cout<<"5. IsFull";
cout<<"6. Exit";
cout<<"\nEnter any operation";
cin>>ans;
switch(ans){
case 1 :
cout<<"Enter an item";
cin>>item;
mystack.push(item);
break ;
case 2 :
cout<<"The element is "<<mystack.pop();
break ;
case 3 :
cout<<"The topmost element is "<<mystack.peep();
break ;
case 4 :
cout<<mystack.isEmpty();
break ;
case 5 :
cout<<mystack.isFull();
break ;
case 6 :
exit(0);
break ;
default:
cout<<"Invalid input";
exit(0);
}
}
return 0;
}
It gives 6 errors :
1 2 3 4 5 6
undefined reference to Stack::Stack()
undefined reference to Stack::pop()
undefined reference to Stack::push(int)
undefined reference to Stack::peep()
undefined reference to Stack::isEmpty()
undefined reference to Stack::isFull()
All the files Stack.cpp , Stack.h , Stack.o , main.cpp are within the same directory Stack.
Still , it is giving these reference errors .
If I uncomment the line #include<Stack.cpp> , it works. However it should work only with #include<Stack.h>
Any ideas ?!!!
No, it shouldn't work without stack.cpp! How should it work without stack.cpp when all the definitions are in there? You should remove line 1 from stack.cpp and in the last line of stack.h, before the #endif, put #include <Stack.cpp>.
Moreover , the
#ifndef is used to check whether the header file has been defined before .
If not defined , then it is defined and thus compiles only once
#endif ends the expression.
Here is how C++ deals with more than one cpp file needed to make a single executable or library; each cpp file is compiled separately and then linked together by the linker. If you #include each cpp file into one giant file you've wrecked the point of having separate files and once your code is more than a few files in size you'll start hitting problems.
If you're using some kind of IDE and you don't know what a linker is, that's unfortunate and something you should fix. In the meantime, I expect what you need to do is add the file stack.cpp to your "project" or whatever your IDE calls it.
You need to tell your IDE that you want it to compile the separate cpp files and then link them together. This is done by putting them in the same "project" or whatever your IDE calls such groups of source code that is all for linking together.
Edit: As an aside, for any regulars reading this thread, this sort of thing is exactly why I advocate that beginners shouldn't use an IDE.
At last , I did it.
1. Reinstalled Codeblocks and
2. Created a new project on Desktop.
3. Added files to the project.
It creates an additional file called Stacks.depend which contains the information regarding linking of header files with the source files.
thanks guys for replying.