Placing classes in separate files

Hi there ,
I have tried to create a separate file for the class Stack , however it gives the following error :
undefined reference for 'WinMain@16'

Here are the files:
Stack.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef STACK_H
#define STACK_H
#define arrsize 10

class Stack
{
    private:
        int top ;
        int arr[arrsize];

    public:
        Stack();
        void push(int);
        int pop();
        bool isEmpty();
        bool isFull();
        int peep();
};

#endif // STACK_H 


Can anyone please explain the lines as the file is automatically generated by CodeBlocks ?
1
2
3
#ifndef STACK_H
#define STACK_H
#endif // STACK_H 


Stack.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include "stack.h"
#include<iostream>
#include<cstdlib>
using namespace std ;

Stack::Stack() {
    top = -1 ;
}

void Stack::push(int item){
    if(top == (arrsize-1)){
        cout<<"Stack is full";
        exit(0);
    }
    else{
        top += 1;
        arr[top] = item ;
    }
}

int Stack::pop(){
    if(top == -1 ){
        cout<<"Stack is empty";
        return NULL;
    }else{
        int item = arr[top];
        top -= 1 ;
        return item;
    }
}

bool Stack::isEmpty(){
    if(top == -1){
        return true ;
    }else{
        return false;
    }
}

bool Stack::isFull(){
    if(top == (arrsize - 1)){
        return true ;
    }else {
        return false ;
    }
}

int Stack::peep(){
    if(top = -1){
        return NULL;
    }else {
        return arr[top];
    }
}


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
The error has been resolved , I had to include
1
2
3
int main(){
  return 0 ;
}

in the implementation file.

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.
Last edited on
Here is a new problem :
Implementation file : main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include<iostream>
#include<cstdlib>
#include "stack.h"
//#include "stack.cpp"
using namespace 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.

Well, doh!!!
Last edited on
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.
Last edited on
I am using CodeBlocks IDE and Mingw compiler . I have not created a project or something . These are separate files :
 
All the files Stack.cpp , Stack.h , Stack.o , main.cpp are within the same directory Stack.


This is what I am trying to do ..
http://www.youtube.com/watch?v=NTip15BHVZc&list=PLAE85DE8440AA6B83&index=15&feature=plpp_video


I have not created a project or something


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.
Last edited on
What should one use if not an IDE ?
There a problem with my IDE . It is unable to create any projects,
even after reinstall.
If I were compiling your code at the command line, I would type

g++ stack.cpp main.cpp

Depending on how you've set up MinGW, that may or may not work.
Last edited on
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.
Topic archived. No new replies allowed.