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里。等有时间再看看问题究竟出现在哪。

  • 相关阅读:
    titanium开发教程0107分组和嵌套view
    titanium开发教程0103理解windows和views
    titanium开发教程0202创建按钮
    titanium开发教程0106理解 ZDepth
    titanium开发教程0204创建开关
    titanium开发教程0201监听事件
    Flex更改Image
    R语言中统计数据框中指定字符出现的次数
    linux shell实现将匹配字符行的最后一个字符替换为指定字符
    plink 软件中 updatemap 命令
  • 原文地址:https://www.cnblogs.com/indifferent/p/13643104.html
Copyright © 2011-2022 走看看