堆栈链接错误问题

发布于 2024-10-16 16:59:42 字数 2303 浏览 4 评论 0原文

当我运行程序时,编译器会给出这些错误 -

  "Stack<int>::push(int&)", referenced from:
      _main in main.o
  "Stack<int>::~Stack()", referenced from:
      _main in main.o
      _main in main.o
  "Stack<int>::Stack()", referenced from:
      _main in main.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

编译时我没有收到任何错误。

代码如下 -

/*
 *  Stack.h
 *  Stack
 *
 *  Created by Sterling McLeod on 2/1/11.
 *  Copyright 2011 University of North Carolina at Charlotte. All rights reserved.
 *
 */
#ifndef STACK_H
#define STACK_H

#include <exception>

template<typename T>
class Stack {

public:


    class EmptyTreeException : public std::exception {
    public:
        virtual const char* what() const throw();
    };  //end exception



    Stack();
    ~Stack();

    T peek();
    bool isEmpty();
    int size();

    void push(T&);
    T pop();

private:
    T* top; 
    int count;
};
#endif STACK_H
[/code]
[code]
/*
 *  Stack.cpp
 *  Stack
 *
 *  Created by Sterling McLeod on 2/1/11.
 *  Copyright 2011 University of North Carolina at Charlotte. All rights reserved.
 *
 */

#include "Stack.h"

template <typename T>
const char* Stack<T>::EmptyTreeException::what() const throw() {
    return "The stack is empty!\n";
}


template <typename T>
Stack<T>::Stack() : count(0) {}
template <typename T>
Stack<T>::~Stack() {delete [] top;}


template <typename T>
T Stack<T>::peek() {return top[count];}

template <typename T>
bool Stack<T>::isEmpty() {return count == 0;}

template <typename T>
int Stack<T>::size() {return count;}




template <typename T>
void Stack<T>::push(T& n) {
    top[++count] = n;
}   //END PUSH


template <typename T>
T Stack<T>::pop() {
    if(isEmpty())
        throw EmptyTreeException();
    T result = top[count];
    top[count--] = NULL;
    return result;
}   //END POP
[/code]
[code]
#include <iostream>
#include "Stack.h"

int main (int argc, char * const argv[]) {
    Stack<int> s;
    int a = 10;
    while(a < 50) {
        s.push(a);
        a += 10;
    }   //end
    return 0;
}

我不知道为什么这不起作用......看起来它对我来说应该没有问题。如果有人能给我一个提示那就太好了。谢谢。

The compiler gives me these errors when I run my program -

  "Stack<int>::push(int&)", referenced from:
      _main in main.o
  "Stack<int>::~Stack()", referenced from:
      _main in main.o
      _main in main.o
  "Stack<int>::Stack()", referenced from:
      _main in main.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

I get no errors when I compile.

The code is the following -

/*
 *  Stack.h
 *  Stack
 *
 *  Created by Sterling McLeod on 2/1/11.
 *  Copyright 2011 University of North Carolina at Charlotte. All rights reserved.
 *
 */
#ifndef STACK_H
#define STACK_H

#include <exception>

template<typename T>
class Stack {

public:


    class EmptyTreeException : public std::exception {
    public:
        virtual const char* what() const throw();
    };  //end exception



    Stack();
    ~Stack();

    T peek();
    bool isEmpty();
    int size();

    void push(T&);
    T pop();

private:
    T* top; 
    int count;
};
#endif STACK_H
[/code]
[code]
/*
 *  Stack.cpp
 *  Stack
 *
 *  Created by Sterling McLeod on 2/1/11.
 *  Copyright 2011 University of North Carolina at Charlotte. All rights reserved.
 *
 */

#include "Stack.h"

template <typename T>
const char* Stack<T>::EmptyTreeException::what() const throw() {
    return "The stack is empty!\n";
}


template <typename T>
Stack<T>::Stack() : count(0) {}
template <typename T>
Stack<T>::~Stack() {delete [] top;}


template <typename T>
T Stack<T>::peek() {return top[count];}

template <typename T>
bool Stack<T>::isEmpty() {return count == 0;}

template <typename T>
int Stack<T>::size() {return count;}




template <typename T>
void Stack<T>::push(T& n) {
    top[++count] = n;
}   //END PUSH


template <typename T>
T Stack<T>::pop() {
    if(isEmpty())
        throw EmptyTreeException();
    T result = top[count];
    top[count--] = NULL;
    return result;
}   //END POP
[/code]
[code]
#include <iostream>
#include "Stack.h"

int main (int argc, char * const argv[]) {
    Stack<int> s;
    int a = 10;
    while(a < 50) {
        s.push(a);
        a += 10;
    }   //end
    return 0;
}

I have no idea why this won't work...it seems like it should work no problem to me. If anyone can give me a hint that would be great. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

真心难拥有 2024-10-23 16:59:43

简而言之:模板定义应该放在头文件中,否则编译器不会实例化它们,链接器会哭。

有关更多详细信息,请参阅 http://www.parashift.com/ c++-faq-lite/templates.html#faq-35.12

In short: Template definitions should go in the header file, otherwise the compiler won't instantiate them, and the linker will cry.

For more details, see http://www.parashift.com/c++-faq-lite/templates.html#faq-35.12.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文