0

可能重复:
为什么模板只能在头文件中实现?

当我将 Stack.cpp 放入 Stack.h 时,它工作得很好,但是当我分离 Stack.h,.cpp 文件时,它给出了这个错误。我还有一个 main.cpp 文件,它只包含 AlgebraicExpression.h 我使用此命令进行编译:“g++ -o main main.cpp AlgebraicExpression.cpp Stack.cpp”

Undefined symbols for architecture x86_64:   "Stack<char>::pop()", referenced from:
      infix2postfix(char*) in ccKgncmm.o   "Stack<char>::top()", referenced from:
      infix2postfix(char*) in ccKgncmm.o   "Stack<char>::push(char)", referenced from:
      infix2postfix(char*) in ccKgncmm.o   "Stack<char>::size()", referenced from:
      infix2postfix(char*) in ccKgncmm.o   "Stack<char>::Stack()", referenced from:
      infix2postfix(char*) in ccKgncmm.o   "Stack<double>::pop()", referenced from:
      evaluatePostfix(char*) in ccKgncmm.o   "Stack<double>::top()", referenced from:
      evaluatePostfix(char*) in ccKgncmm.o   "Stack<double>::push(double)", referenced from:
      evaluatePostfix(char*) in ccKgncmm.o   "Stack<double>::Stack()", referenced from:
      evaluatePostfix(char*) in ccKgncmm.o ld: symbol(s) not found for architecture x86_64 collect2: ld returned 1 exit status

代数表达式.h

char* infix2postfix(char *infix);
double evaluatePostfix(char*);

堆栈.h

#ifndef STACK_H
#define STACK_H

template <class T>
class Stack
{
public:
        Stack();
        void pop();
        T top();
        void push(T val);
        int size();

        T *values;
        int maxSize;
        int length;
};

#endif

代数表达式.cpp

#include "AlgebraicExpression.h"
#include "Stack.h"
using namespace std;
bool isOperator(char c)
{
        if(c=='+' || c=='-' ||
                c=='*' || c=='/')
                return true;
        return false;
}

bool isDigit(char c)
{
        if(c=='0'||(c>='1'&&c<='9'))
                return true;
        return false;
}

int compareOperators(char c1,char c2)
{
        int v1,v2;
        if(!isOperator(c1) || !isOperator(c2))
                return -1;
        if(c1 == '*' || c1 =='/')v1 = 1;
        else v1 = 0;
        if(c2 == '*' || c2 == '/')v2 = 1;
        else v2 = 0;

        return v1-v2;
}

char *infix2postfix(char *infix)
{
        int lenIn,lenPost;
        for(lenIn=0,lenPost=0;infix[lenIn];++lenIn)
                if(infix[lenIn]!='('&&infix[lenIn]!=')')
                        ++lenPost;

        char *postfix = new char[lenPost+1];
        int i,j;
        Stack<char> operations;

        for(i=0,j=0;i<lenIn&&j<lenPost;++i)
                if(isDigit(infix[i]))
                        postfix[j++] = infix[i];
                else if(isOperator(infix[i]))
                {
                        while(operations.size()&&
                                compareOperators(operations.top(),infix[i])>-1)
                        {
                                postfix[j++] = operations.top();
                                operations.pop();
                        }
                        operations.push(infix[i]);
                }
                else
                {
                        if(infix[i] == '(')
                                operations.push(infix[i]);
                        else if(infix[i] ==')')
                        {
                                while(operations.size()&&operations.top()!='(')
                                {
                                        postfix[j++] = operations.top();
                                        operations.pop();

                                }
                                operations.pop();
                        }
                }
        while(operations.size())
        {
                postfix[j++] = operations.top();
                operations.pop();
        }
        postfix[j] = '\0';
        return postfix;
}

double evaluatePostfix(char *postfix)
{
        Stack<double> result;
        for(int i=0;postfix[i];++i)
                if(isDigit(postfix[i]))
                        result.push(postfix[i]-'0');
                else
                {
                        double n1,n2,r;
                        n1 = result.top();
                        result.pop();
                        n2 = result.top();
                        result.pop();
                        if(postfix[i] == '+')
                                r = n1+n2;
                        else if(postfix[i] == '-')
                                r = n2-n1;
                        else if(postfix[i] == '*')
                                r = n1*n2;
                        else if(postfix[i] == '/')
                                r = n1/n2;
                        result.push(r);
                }
        return result.top();
}

堆栈.cpp

#include "Stack.h"
template <class T>
Stack<T>::Stack()
{
        maxSize = 100;
        length =0;
        values = new T[maxSize];
}

template <class T>
T Stack<T>::top()
{
        return values[length-1];
}

template <class T>
void Stack<T>::push(T val)
{
        if(maxSize==length)
        {
                T *temp = new T[length*2];
                for(int i=0;i<length;++i)
                        temp[i] = values[i];
                values = temp;
                maxSize = length*2;
        }
        values[length++] = val;
}

template <class T>
void Stack<T>::pop()
{
        if(length>0)
                length--;
}

template <class T>
int Stack<T>::size()
{
        return length;
}
4

2 回答 2

4

使用类模板,所有定义都需要在标题本身中。

C++ FAQ是一本很好的读物。

基本上,当一个模板类被实例化时,它需要能够看到类的整个定义而不仅仅是标题。

如果您知道您的类将使用的所有类型,则可以使用所有这些类型实例化模板,然后将定义保留在 cpp中,如此 C++ FAQ 条目中所述

如前所述,为什么模板只能在头文件中实现?

于 2013-01-03T05:20:19.703 回答
2

如果你真的想分离模板头和实现文件,你可以包含Stack.cpp在底部Stack.h,它被称为The Inclusion Model

#include "Stack.cpp"

查看参考: 为什么模板只能在头文件中实现?

于 2013-01-03T05:37:33.210 回答