关于模板:C ++不断收到错误LNK2019:未解析的外部符号

C++ keep getting error LNK2019: unresolved external symbol

本问题已经有最佳答案,请猛点这里访问。

我试着用谷歌搜索这个,但总是会遇到不同的问题。当我试图编译这个程序时,我得到了3个未解决的外部问题:

1
2
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

DytStang.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/****************************************************************************
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

DYNTITSTACK.CPP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/****************************************************************************
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.
"
;
   }
   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;
}

主CPP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include"DynIntStack.h"
using namespace std;

int main(){

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

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

    system("pause");
    return 0;
}

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


在dynintstack.h的底部,

1
#include <DynIntStack.cpp>

现在的情况是编译器没有看到模板实现代码,因此不能为它发出任何东西。


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

在任何需要包含模板类的地方,例如"dynintstack.h"改为执行"dynintstack.cpp"。例如,在上面的main.cpp中。

简单的说,不需要改变任何其他东西。