Reputation: 1
I"m having some problem with the following program. The program implements a stack using a linked list. I'm not showing all my code here because the code is fine. But the problem I'm having is with linking different files together.
I'm using an IDE to run the program. When I run the TestIntStacks.cpp
, the main method is supposed to call test()
from StackFunctions.cpp
. The test function (defined in StackFunctions.cpp
), uses the TestStack
class methods.
Currently I'm receiving an error saying "linker error, push/pop not defined". What I'm doing wrong? I'm sure it's something to do with a namespace.
MyStack.h
-------------------------------------
namespace A
{
class Node{
public :
char data;
StackNode* link;
StackNode(int v=0): data(v), link(NULL){ }
};
class MyStack{
private:
Node * top;
public:
MyStack():top(NULL){ }
void push(int c);
};
}//namespace
//TestStack.cpp
--------------------------------------------------------------
#include "MyStack.h"
namespace A
{
void MyStack::push(int x)
{
StackNode *temp = new StackNode(x);
temp->link = top;
top = temp;
}
}
//StackFunctions.cpp
-----------------------------------------------------------
#include <iostream>
#include "TestStack.h"
using namespace std;
using namespace A;
void test()
{
MyStack st;
st.push(1);
st.push(2);
st.push(3);
st.push(4);
}
// TestIntStacks.cpp
----------------------------------------------------------------
// Code for testing the TestStack
// from the A namespace.
#include <iostream>
#include <vector>
using namespace std;
#include "TestStack"
#include "StackFunctions.cpp"
void test();
int main()
{
test();
system("pause");
return 0;
}
Upvotes: 0
Views: 247
Reputation: 11
I think it has to do with the arguments provided to linker. For example, a similar error occurs when you use Visual C++ 6 in a following way. Let's say you created .cpp and .h files for a class. If you do not include cpp file into your project you get the similar error. Because the IDE does not determine the source file based on the provided header file. I don't know about dev-c++ IDE, but the solution might be similar. The problem is you compile (or not) TestStack.cpp and the output of this compiling is not provided to the linker, so the linker can't find the implementation.
Upvotes: 1
Reputation: 26251
You need to force the build script to use both cpp files. If you wrote your own make file, you need to build intermediate objects for each source, and then link at the end.
I suspect DEV-C++ doesnt automatically generate object files or try to link everything together.
Upvotes: 0
Reputation: 67175
This error seems pretty clear to me. You declared push()
and pop()
in your header file, but the linker could not find where these methods are implemented.
Where are they defined?
Upvotes: 1
Reputation: 14177
You are defining push()
and pop(
) methods in your header file TestStack.h, but you've not provided implementations for them in TestStack.cpp. You need to add the code that does the push and pop operations on your object.
Upvotes: 1