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!

is the stack.cpp also having a main?

is the stack.cpp also having a main?

Yes, but it doesn't contain anything. I just put it there so that it can be compiled.
Thank's.

delete main() out of stack.cpp and everything links ok for me. Using VC++ 2008 Express.
Do you have stack.cpp part of our compiler's project ?

delete main() out of stack.cpp and everything links ok for me. Using VC++ 2008 Express.
Do you have stack.cpp part of our compiler's project ?

Thanks a lot!
I forgot to include all necessary files in the compiler's project, that's why when I have main() in stack.cpp this file individually can be compiled.
It's OK now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.