简体   繁体   English

通过调用模板类的成员函数引用符号的错误

[英]Symbol referencing errors from calling a member function of a template class

I'm writing a program that uses stacks to evaluate infix expressions read in from a file. 我正在编写一个程序,该程序使用堆栈来评估从文件读取的中缀表达式。 Here is the code: 这是代码:

ptStack.h ptStack.h

#ifndef STACK
#define STACK

#include <cstdlib>
#include <iostream>
using namespace std;

template <class Item>
class Stack
{
public:
    // CONSTRUCTOR
    Stack( ) {top = NULL; count = 0;}       // Inline

    // MODIFIER MEMBER FUNCTIONS
    void push( const Item& entry);
    Item pop( );

    // CONSTANT MEMBER FUNCTIONS
    int size( ) {return count;}             // Inline
    bool is_empty( ) {return count == 0;}   // Inline

private:
    // DATA MEMBERS
    struct Node
    {
        Item element;
        Node *next;
    };
    Node *top;
    int count;
};
#endif

ptStack.cpp ptStack.cpp

    #include <cassert>
#include "ptStack.h"
using namespace std;

// MODIFIER MEMBER FUNCTIONS
template <class Item> 
void Stack<Item>::push(const Item& entry)
{
    Node *temp;
    temp = new Node;
    temp->element = entry;
    temp->next = top->next;
    top->next = temp;
    count++;
}

template <class Item> 
Item Stack<Item>::pop( )
{
    Item value;
    Node *temp;
    value = top->next->element;
    temp = top->next;
    top->next = top->next->next;
    delete temp;
    count--;

    return value;
}

infix.cpp infix.cpp

#include <iostream>
#include <fstream>
#include <cstdlib>
#include "ptStack.h"
using namespace std;

// PRECONDITION: op must be an operator
// POSTCONDITION: returns precedence of operator
int pr(char op)
{
    int precedence = 0;
    switch (op)
    {
        case '+':
        case '-': precedence = 1;
        case '*':
        case '/': precedence = 2;
        default : precedence = 0;
    }
    return precedence;
}

// PRECONDITIONS: optr is one of the following: + - * /
//                opnd1 and opnd2 are numbers from 1-9
// POSTCONDITIONS: returns the result of the chosen mathematical operation
int apply(char optr, int opnd1, int opnd2)
{
    int result;
    switch (optr)
    {
        case '+': result = opnd2+opnd1;
        case '-': result = opnd2-opnd1;
        case '*': result = opnd2*opnd1;
        default : result = opnd2/opnd1;
    }
    return result;
}

int main()
{
    Stack<int> numbers;
    Stack<char> operators;
    char ch, optr;
    int num, opnd1, opnd2, prec = 0, newprec;
    ifstream in_file;                   // input file
    char in_file_name[20];              // name of input file (20 letter max)

    cout << "Enter input file name: ";
    cin >> in_file_name;
    in_file.open(in_file_name);         // opens file to read equations

    while (!in_file.eof())
    {
            cout << "Expression: ";

    while(in_file >> ch)
    {
        if (ch == ' ')
        {}
        else
        {
            num = ch - '0';
            if((num < 10) && (num > 0))
            {
                cout << num << " ";
                numbers.push(num);
            }
            else if((ch == '+') || (ch == '-') || (ch == '*') || (ch == '/'))
            {
                cout << ch << " ";
                newprec = pr(ch);
                if(newprec >= prec)
                {
                    prec = newprec;
                    operators.push(ch);
                }
                else if(newprec < prec)
                {
                    optr = operators.pop( );
                    opnd1 = numbers.pop( );
                    opnd2 = numbers.pop( );
                    num = apply(optr, opnd1, opnd2);
                    numbers.push(num);
                    operators.push(ch);
                }
            }
        }
        if(in_file.peek() == '\n')
            break;
    }

    num = operators.size();
    while(num != 0)
    {
        optr = operators.pop( );
        opnd1 = numbers.pop( );
        opnd2 = numbers.pop( );
        num = apply(optr, opnd1, opnd2);
        numbers.push(num);

        num = operators.size( );
    }
    num = numbers.pop( );
    cout << endl << "Value = " << num << endl << endl; 
    }

    return 0;
}

It looks like everything should work but when I compile it, I get this error message. 看起来一切正常,但是当我编译它时,我收到此错误消息。

> g++ -c ptStack.cpp
> g++ infix.cpp ptStack.o

Undefined                       first referenced
 symbol                             in file
_ZN5StackIcE4pushERKc               /var/tmp//ccwhfRAZ.o
_ZN5StackIiE4pushERKi               /var/tmp//ccwhfRAZ.o
_ZN5StackIcE3popEv                  /var/tmp//ccwhfRAZ.o
_ZN5StackIiE3popEv                  /var/tmp//ccwhfRAZ.o
ld: fatal: symbol referencing errors. No output written to a.out

I've been able to pinpoint the errors to the callings of the push and pop member functions in the main of the infix file. 我已经能够在infix文件的主目录中将错误定位到push和pop成员函数的调用。 I tried defining them inline in the header file like the size function and it compiles just fine using g++ infix.cpp ptStack.h, but then I get a segmentation fault when I run it. 我尝试在头文件中像size函数一样内联定义它们,并且使用g ++ infix.cpp ptStack.h可以很好地进行编译,但是当我运行它时遇到了分段错误。

Does anyone know how to fix either of these errors? 有谁知道如何解决这些错误之一? Thanks in advance. 提前致谢。

只需使用g++编译所有.cpp文件

g++ ptStack.cpp infix.cpp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM