C++不断收到错误LNK2019:无法解析的外部符号
问题描述:
我试图谷歌这个,但总是回来不同的问题。我得到3层解析的外部,当我尝试编译这个程序:C++不断收到错误LNK2019:无法解析的外部符号
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall DynIntStack<char>::~DynIntStack<char>(void)" ([email protected]@@[email protected]) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "public: void __thiscall DynIntStack<char>::pop(char &)" ([email protected][email protected]@@[email protected]) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "public: void __thiscall DynIntStack<char>::push(char)" ([email protected][email protected]@@[email protected]) 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 T>
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 <iostream>
#include "DynIntStack.h"
using namespace std;
/*************************************************************************
Basic class constructor.
Input Parameters: Information to build the stack
Return Type: void
*************************************************************************/
template<class T>
DynIntStack<T>::~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<class T>
void DynIntStack<T>::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<class T>
void DynIntStack<T>::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<class T>
bool DynIntStack<T>::isEmpty()
{
bool status;
if (!top)
status = true;
else
status = false;
return status;
}
的main.cpp
#include <iostream>
#include "DynIntStack.h"
using namespace std;
int main(){
int value = 0;
char value2;
//DynIntStack<int> stack;
DynIntStack<char> stack1;
cout << "Pushing 1\n";
stack1.push('T');
stack1.pop(value2);
cout << value2;
system("pause");
return 0;
}
答
您需要将您在.cpp文件中的所有模板实现放在头文件或头文件包含的文件中。并且不要尝试编译实现文件。某些系统试图编译带有.cpp后缀的文件。编译器需要查看代码才能实例化模板。
答
在DynIntStack.h的底部,把
#include <DynIntStack.cpp>
发生了什么事时,编译器不会看到模板实现代码,从而不能为发出任何东西。
答
上述问题的最简单的解决方法是:
不管你希望在你的模板类如“DynIntStack.h” 改为“DynIntStack.cpp”。例如在你的main.cpp上面。
就这么简单,无需改变其他任何东西。
如果我对每次我看到这个问题 – EdChum 2012-04-22 20:27:30
@EdChum或:-) – juanchopanza 2012-04-22 20:27:59
啊的给予好评一斤,你打我吧 – stijn 2012-04-22 20:28:06