Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;不断获取错误LNK2019:未解析的外部符号_C++_Templates_Linker - Fatal编程技术网

C++ C++;不断获取错误LNK2019:未解析的外部符号

C++ C++;不断获取错误LNK2019:未解析的外部符号,c++,templates,linker,C++,Templates,Linker,我试着用谷歌搜索这个,但每次都会出现不同的问题。当我试图编译此程序时,我得到3个未解析的外部: 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

我试着用谷歌搜索这个,但每次都会出现不同的问题。当我试图编译此程序时,我得到3个未解析的外部:

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
1>main.obj:错误LNK2019:未解析的外部符号“public:\u thiscall DynIntStack::~DynIntStack(void)”(??1$DynIntStack@D@@QAE@XZ)在函数_main中引用
1> main.obj:错误LNK2019:未解析的外部符号“public:void\u thiscall dyninstack::pop(char&)”(?pop@$DynIntStack@D@@QAEXAAD@Z)在函数_main中引用
1> main.obj:错误LNK2019:未解析的外部符号“public:void\u thiscall DynIntStack::push(char)”(?push@$DynIntStack@D@@QAEXD@Z)在函数_main中引用
dyninstack.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
/****************************************************************************
动态堆栈类。
乍得辣椒
此类创建用于堆叠节点的对象
此外,应该有成员函数来执行以下操作
操作:
-一鼓作气
-跳到堆栈中
-函数检查是否为空
****************************************************************************/
//DyntStack类的规范文件
#ifndef DYNINTU H
#定义动态堆栈
模板
类堆栈
{
私人:
//堆栈节点的结构
结构堆栈节点
{
T value;//节点中的值
StackNode*next;//指向下一个节点的指针
};
StackNode*top;//指向堆栈顶部的指针
公众:
//建造师
DynIntStack()
{top=NULL;}
//析构函数
~DynIntStack();
//堆栈操作
空隙推力(T);
无效pop(T&);
bool是空的();
}; 
#恩迪夫
dyninstack.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;
}
#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;
}
/****************************************************************************
动态堆栈类。
乍得辣椒
此类创建用于堆叠节点的对象
此外,应该有成员函数来执行以下操作
操作:
-一鼓作气
-跳到堆栈中
-函数检查是否为空
****************************************************************************/
#包括
#包括“DynIntStack.h”
使用名称空间std;
/*************************************************************************
基本类构造函数。
输入参数:用于构建堆栈的信息
返回类型:void
*************************************************************************/
模板
DynIntStack::~DynIntStack()
{
StackNode*nodePtr,*nextNode;
//将nodePtr放置在堆栈顶部。
nodePtr=top;
//遍历列表,删除每个节点。
while(nodePtr!=NULL)
{
nextNode=nodePtr->next;
删除nodePtr;
nodePtr=nextNode;
}
}
/*************************************************************************
函数将项目推送到堆栈中
输入参数:T
返回类型:void
*************************************************************************/
模板
void DynIntStack::push(T num)
{
StackNode*newNode;//指向新节点的指针
//分配一个新节点并将num存储在那里。
newNode=新StackNode;
newNode->value=num;
//如果列表中没有节点
//使newNode成为第一个节点。
if(isEmpty())
{
top=newNode;
newNode->next=NULL;
}
else//否则,在top之前插入NewNode。
{
新建节点->下一步=顶部;
top=newNode;
}
}
/*************************************************************************
函数在堆栈中弹出一个项
输入参数:T
返回类型:void
*************************************************************************/
模板
void DynIntStack::pop(T&num)
{
StackNode*temp;//临时指针
//首先确保堆栈不是空的。
if(isEmpty())
{
库特值;
温度=顶部->下一步;
删除顶部;
顶部=温度;
}
}
/*************************************************************************
基本类解构器。
输入参数:无
返回类型:void
*************************************************************************/
模板
bool DynIntStack::isEmpty()
{
布尔状态;
如果(!顶部)
状态=真;
其他的
状态=假;
返回状态;
}
main.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;
}
#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;
}
#包括
#包括“DynIntStack.h”
使用名称空间std;
int main(){
int值=0;
字符值2;
//动态堆栈;
dynintstack1;

cout您需要将.cpp文件中的所有模板实现放在头文件中,或者放在头文件中包含的文件中。不要尝试编译实现文件。有些系统尝试编译带有.cpp后缀的文件。编译器需要查看代码才能实例化模板。

在代码底部h,放

#include <DynIntStack.cpp>
#包括

发生的情况是编译器没有看到模板实现代码,因此无法为其发出任何信息。

解决此问题的最简单方法是:

无论您希望在何处包含模板类,例如“dyninstack.h” 改为使用“DynIntStack.cpp”。例如,在上面的main.cpp中


就这么简单,不需要改变任何其他东西。

如果每次我看到这个,我都有一磅question@EdChum或者一次投票:-)@juanchopanza是的,是我投票给你的,似乎我们每2-3天就会收到这些模板链接器错误非常令人沮丧!这只是模板上的错误?@Chad是的。将模板视为需要修改的部分代码编译时的特定模板参数,以便实例化。编译器基本上是用模板生成代码。这必须在编译之前发生。我希望这是有意义的…为什么这个答案被否决了,因为解决方案确实有效?其他答案也说了同样的话,而且是2年前的答案。