zoukankan      html  css  js  c++  java
  • C++ error LNK2019: 无法解析的外部符号

    问题描述:1.用Stack.h描述Stack类的定义,用Stack.cpp实现Stack.h定义的函数,在9_9.cpp里调用,但是编译时会报 “error LNK2019: 无法解析的外部符号”的错

    1.Stack.h的代码如下

    //Stack.h
    #ifndef STACK_H
    #define STACK_H
    #include<iostream>
    #include<cassert>
    template<class T,int SIZE=50>
    class Stack {
    private:
        T list[SIZE];
        int top;
    public:
        Stack();
        void push(const T& item);
        T pop();
        void clear();
        const T& peek()const;
        bool isEmpty()const;
        bool isFull()const;
    };
    
    #endif // !STACK_H

    2.Stack.cpp代码如下

     1 #include"Stack.h"
     2 #include<iostream>
     3 using namespace std;
     4 template<class T, int SIZE>
     5 Stack<T, SIZE>::Stack() {
     6     top = -1;
     7 }
     8 
     9 template<class T, int SIZE>
    10 void Stack<T, SIZE>::push(const T& item) {
    11     assert(!isFull());
    12     top++;
    13     list[top] = item;
    14 }
    15 
    16 template<class T, int SIZE>
    17 T Stack<T, SIZE>::pop() {
    18     assert(!isEmpty());
    19     return list[top--];
    20 }
    21 
    22 template<class T, int SIZE>
    23 const T& Stack<T, SIZE>::peek() const {
    24     assert(!isEmpty());
    25     return list[top];
    26 }
    27 
    28 template<class T, int SIZE>
    29 void Stack<T, SIZE>::clear() {
    30     top = -1;
    31 }
    32 
    33 template<class T, int SIZE>
    34 bool Stack<T, SIZE>::isEmpty()const {
    35     return top == -1;
    36 }
    37 
    38 template<class T, int SIZE>
    39 bool Stack<T, SIZE>::isFull()const {
    40     return top == SIZE - 1;
    41 }

    3.9_9.cpp代码如下

    #include<iostream>
    #include"Stack.h"
    
    using namespace std;
    
    int main() {
        /*Calculator c;
        c.run();*/
        Stack<double,50> s;
        s.push(3.3);
        cout << s.pop() << endl;
        return 0;
    }

    网上很多说是没有引进库或模板和模板类的定义不在同一文件夹下面,然而试了半天还是出现同样的错误,百思不得其解。

    4)暂时解决方法,将Stack.cpp的函数定义写在Stack.h里。等有时间再看看问题究竟出现在哪。

  • 相关阅读:
    Ubuntu下errno值
    Git 经常使用命令总结
    【我们都爱Paul Hegarty】斯坦福IOS8公开课个人笔记38 Unwind Segue反向过渡
    高斯噪声
    小记5.8面试
    基数排序之多keyword排序运用队列
    广告贴
    输入字符串反序输出
    Codeforces Round #313 A. Currency System in Geraldion
    matlab中怎样加入凝视
  • 原文地址:https://www.cnblogs.com/indifferent/p/13643104.html
Copyright © 2011-2022 走看看