Hi, I'm a beginner in C++. I have a problem with referencing resources between 2 or more files.
This is the main cpp code:
#include "stack.h"
#include <cassert>
#include <iostream>
using std::cout;
using std::endl;
int main()
{
IStack ist;
ist.Push(1);
}
and the header file:
const int maxStack = 16;
class IStack
{
public:
IStack () :_top (0) {}
void Push (int i);
int Pop ();
int Top () const;
int Count() const;
private:
int _arr [maxStack];
int _top;
};
I place the function's implementation in a separate cpp file named stack.cpp.
stack.cpp code:
#include "stack.h"
#include <cassert>
#include <iostream>
using std::cout;
using std::endl;
void IStack::Push (int i)
{
assert (_top < maxStack);
_arr [_top] = i;
++_top;
}
int IStack::Pop ()
{
assert (_top > 0);
--_top;
return _arr [_top];
}
int IStack::Count () const
{
return _top;
}
int IStack::Top () const
{
assert (_top > 0);
return _arr [_top - 1];
}
int main ()
{
}
When I compile the above main cpp code, I get a Linker Error (undefined reference to 'IStack::Push(int)').
(PS: when I put the functions implementation to the main cpp file, it compiles).
What is the cause of the linker error when I use separate files?
I really appreciate your help!