2012-04-22 69 views
6

私はこれをgoogleにしようとしましたが、いつも別の問題で戻ってきました。私はこのプログラムのコンパイルしようとすると、私は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; 
} 

答えて

13

.cppファイルにあるすべてのテンプレート実装は、ヘッダーファイルまたはヘッダーに含まれるファイルに配置する必要があります。そして、実装ファイルをコンパイルしようとしないでください。一部のシステムでは、.cppという接尾辞を持つファイルをコンパイルしようとします。コンパイラーは、テンプレートをインスタンス化するためにコードを参照する必要があります。

DynIntStack.hの下部に
+1

私は毎回のためにポンドを持っていた場合、私はこの質問 – EdChum

+0

@EdChumか、ああ、あなたがうん@juanchopanzaそれに – juanchopanza

+0

:-) upvoteは、「それはあなたをupvoted、我々が得るように見えないことを私にTIS見ますこれらのテンプレートリンカーのエラーは2〜3日おきに – stijn

3

は、何が起こっているのか

#include <DynIntStack.cpp> 

を入れコンパイラは、テンプレートの実装コードがそれのために何を放出することはできません見ていないです。

-2

問題に対する最も簡単な解決策は以下のとおりです。あなたは、例えば、あなたのテンプレートクラスを含めたい場合はいつでも

"DynIntStack.h" 代わりに "DynIntStack.cpp"を実行してください。例えば上記のmain.cppにあります。

他のものを変更する必要はありません。

関連する問題