本文介绍了C ++不断收到错误LNK2019:未解析的外部符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图google这个,但总是回来有不同的问题。当我尝试编译这个程序时,我得到3个未解决的外部:
1> main.obj:error LNK2019:unresolved external symbol public:__thiscall DynintStack< char> ::〜DynIntStack< char>(void)(?? 1?$ DynIntStack @ D @@ QAE @ XZ)函数_main
1> main.obj:error LNK2019 :在函数_main中引用的未解析外部符号public:void __thiscall DynIntStack< char> :: pop(char&)(?pop @?$ DynIntStack @ D @@ QAEXAAD @ Z) :error LNK2019:函数_main中引用的未解析的外部符号public:void __thiscall DynIntStack< char> :: push(char)(?push @?$ DynIntStack @ D @@ QAEXD @ Z)
DynIntStack.h
/ ******************************************** ********************************
DynIntStack类。
Chad Peppers
此类为堆叠节点创建一个对象
此外,还应该有成员函数来执行以下操作
操作:
- push到栈
- 弹出到栈
- 检查空的功能
************ **************************************************** ************** /
// DynIntStack类的规范文件
#ifndef DYNINTSTACK_H
#define DYNINTSTACK_H
模板< class T>
class DynIntStack
{
private:
//堆栈节点的结构
struct StackNode
{
T value; //节点中的值
StackNode * next; //指向下一个节点的指针
};
StackNode * top; //指向栈顶的指针
public:
//构造函数
DynIntStack()
{top = NULL; }
//析构函数
〜DynIntStack();
//堆栈操作
void push(T);
void pop(T&);
bool isEmpty();
}
#endif
DynIntStack.cpp b
$ b/ ***************************** **********************************************
DynIntStack类。
Chad Peppers
此类为堆叠节点创建一个对象
此外,还应该有成员函数来执行以下操作
操作:
- push到栈
- 弹出到栈
- 检查空的功能
************ **************************************************** ************** /
#include< iostream>
#includeDynIntStack.h
using namespace std;
/ ***************************************** ********************************
基本类构造函数。
输入参数:构建栈的信息
返回类型:void
************** *************************************************** ********* /
template< class T>
DynIntStack< T> ::〜DynIntStack()
{
StackNode * nodePtr,* nextNode;
//将nodePtr放在堆栈的顶部。
nodePtr = top;
//遍历删除每个节点的列表。
while(nodePtr!= NULL)
{
nextNode = nodePtr-> next;
delete nodePtr;
nodePtr = nextNode;
}
}
/ ***************************** ******************************************
推送的功能项目在栈中
输入参数:T
返回类型:void
************** **************************************************** ********* /
template< class T>
void DynIntStack< T> :: push(T num)
{
StackNode * newNode; //指向新节点的指针
//分配一个新节点并在其中存储num。
newNode = new StackNode;
newNode-> value = num;
//如果列表中没有节点
//使newNode成为第一个节点。
if(isEmpty())
{
top = newNode;
newNode-> next = NULL;
}
else //否则,在顶部之前插入NewNode。
{
newNode-> next = top;
top = newNode;
}
}
/ ***************************** ******************************************
弹出的功能项目在栈中
输入参数:T
返回类型:void
************** **************************************************** ********* /
template< class T>
void DynIntStack< T> :: pop(T& num)
{
StackNode * temp; //临时指针
//首先确保堆栈不为空。
if(isEmpty())
{
cout<< 堆栈是空的。
}
else //从堆栈顶部弹出值
{
num = top-> value;
temp = top-> next;
delete top;
top = temp;
}
}
/ ***************************** ******************************************
基本类解构函数。
输入参数:无
返回类型:void
****************** **************************************************** ***** /
template< class T>
bool DynIntStack< T> :: isEmpty()
{
bool status;
if(!top)
status = true;
else
status = false;
return status;
}
main.cpp
$ b#include< iostream>
#includeDynIntStack.h
using namespace std;
int main(){
int value = 0;
char value2;
// DynIntStack< int>堆栈
DynIntStack< char> stack1;
cout<< 推1 \\\
;
stack1.push('T');
stack1.pop(value2);
cout<< value2;
系统(pause);
return 0;
}
解决方案您在头文件中的.cpp文件中或头文件包含的文件中的模板实现。并且不要尝试编译实现文件。某些系统尝试使用.cpp后缀编译文件。编译器需要查看代码以实例化模板。
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<char>::~DynIntStack<char>(void)" (??1?$DynIntStack@D@@QAE@XZ) referenced in function _main 1>main.obj : error LNK2019: unresolved external symbol "public: void __thiscall DynIntStack<char>::pop(char &)" (?pop@?$DynIntStack@D@@QAEXAAD@Z) referenced in function _main 1>main.obj : error LNK2019: unresolved external symbol "public: void __thiscall DynIntStack<char>::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 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; }
解决方案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.
这篇关于C ++不断收到错误LNK2019:未解析的外部符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!