Wednesday, 29 March 2017

templates - C++ keep getting error LNK2019: unresolved external symbol





I tried to google this but always come back with different issues. I am getting 3 unresolved externals when I try to compile this program:



1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall DynIntStack::~DynIntStack(void)" (??1?$DynIntStack@D@@QAE@XZ) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "public: void __thiscall DynIntStack::pop(char &)" (?pop@?$DynIntStack@D@@QAEXAAD@Z) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "public: void __thiscall DynIntStack::push(char)" (?push@?$DynIntStack@D@@QAEXD@Z) referenced in function _main


DynIntStack.h



/****************************************************************************

DynIntStack class.

Chad Peppers

This class creates a object for stacking nodes

In addition, there should be member functions to perform the following
operations:
- Push to the stack
- Pop to the stack

- Function to check if empty

****************************************************************************/
// Specification file for the DynIntStack class
#ifndef DYNINTSTACK_H
#define DYNINTSTACK_H

template
class DynIntStack
{

private:
// Structure for stack nodes
struct StackNode
{
T value; // Value in the node
StackNode *next; // Pointer to the next node
};

StackNode *top; // Pointer to the stack top


public:
// Constructor
DynIntStack()
{ top = NULL; }

// Destructor
~DynIntStack();

// Stack operations
void push(T);

void pop(T &);
bool isEmpty();
};
#endif


DynIntStack.cpp



/****************************************************************************
DynIntStack class.


Chad Peppers

This class creates a object for stacking nodes

In addition, there should be member functions to perform the following
operations:
- Push to the stack
- Pop to the stack
- Function to check if empty


****************************************************************************/

#include
#include "DynIntStack.h"
using namespace std;

/*************************************************************************
Basic class constructor.


Input Parameters: Information to build the stack

Return Type: void

*************************************************************************/

template
DynIntStack::~DynIntStack()
{
StackNode *nodePtr, *nextNode;


// Position nodePtr at the top of the stack.
nodePtr = top;

// Traverse the list deleting each node.
while (nodePtr != NULL)
{
nextNode = nodePtr->next;
delete nodePtr;
nodePtr = nextNode;

}
}

/*************************************************************************
Function to push an item in the stack

Input Parameters: T

Return Type: void


*************************************************************************/

template
void DynIntStack::push(T num)
{
StackNode *newNode; // Pointer to a new node

// Allocate a new node and store num there.
newNode = new StackNode;
newNode->value = num;


// If there are no nodes in the list
// make newNode the first node.
if (isEmpty())
{
top = newNode;
newNode->next = NULL;
}
else // Otherwise, insert NewNode before top.
{

newNode->next = top;
top = newNode;
}
}

/*************************************************************************
Function to pop an item in the stack

Input Parameters: T


Return Type: void

*************************************************************************/
template
void DynIntStack::pop(T &num)
{
StackNode *temp; // Temporary pointer

// First make sure the stack isn't empty.
if (isEmpty())

{
cout << "The stack is empty.\n";
}
else // pop value off top of stack
{
num = top->value;
temp = top->next;
delete top;
top = temp;
}

}

/*************************************************************************
Basic class deconstructor.

Input Parameters: None

Return Type: void

*************************************************************************/

template
bool DynIntStack::isEmpty()
{
bool status;

if (!top)
status = true;
else
status = false;


return status;
}


main.cpp



#include 
#include "DynIntStack.h"
using namespace std;


int main(){

int value = 0;
char value2;
//DynIntStack stack;
DynIntStack stack1;

cout << "Pushing 1\n";
stack1.push('T');
stack1.pop(value2);

cout << value2;

system("pause");
return 0;
}

Answer



You need to put all the template implementations that you have in your .cpp file in the header file, or in a file included by the header. And don't try to compile the implementation file. Some systems attempt to compile files with a .cpp suffix. The compiler needs to see the code in order to instantiate templates.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...