BigSpoon 发表于 2016-9-19 10:14:30

关于类模板问题?

如下Stack类.h和.cpp:
template <typename T>
class Stack
{
private:
    enum { MAX = 10};
    int stackSize;
    T* items;
    int top;
public:
    Stack(int ss = MAX);
    Stack(const Stack& st);
    ~Stack() {delete [] items;}
    bool isEmpty();
    bool isFull();
    bool push(const T& item);
    bool pop(T & item);
    Stack & operator=(const Stack& st);
};
template<typename T>
Stack<T>::Stack(int ss):stackSize(ss),top(0)
{
    items = new T;
}

template<typename T>
Stack<T>::Stack(const Stack &st)
{
    stackSize = st.stackSize;
    top = st.top;
    items = new T;
    for(int i = 0; i < top; i++){
      items = st.items;
    }
}

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

template<typename T>
bool Stack<T>::isFull()
{
    return top == MAX ;
}

template<typename T>
bool Stack<T>::push(const T &item)
{
    if(top < MAX){
      items = item;
      return true;
    }
    else
      return false;
}

template<typename T>
bool Stack<T>::pop(T &item)
{
    if(top > 0){
      item = items[--top];
      return true;
    }
    else
      return false;
}
template<typename T>
Stack<T> &Stack<T>::operator=(const Stack &st)
{
    if(this == &st) return *this;
    delete [] items;
    stackSize = st.stackSize;
    top = st.top;
    items = new T;
    for(int i = 0; i < top; i++){
      items = st.items;
    }
    return *this;
}
主函数main如下:
#include <iostream>
#include <cstdlib>
#include <ctime>

#include "stack.h"

using namespace std;
const int Num = 10;

int main()
{
    srand(time(0));
    cout << "## input stack size: ";
    int stackSize;
    cin >> stackSize;
    Stack<const char *> st(stackSize);
    const char* in={
      "one","two","three","four","five",
      "six","seven","eight","nine","ten"
    };
    const char* out;
    int processed = 0;
    int nextin = 0;
    while(processed < Num){
      if(st.isEmpty()){
            st.push(in);
      }else if(st.isFull()){
            st.pop(out);
      }else if( rand() % 2 && nextin < Num){
            st.push(in);
      }else st.pop(out);
    }
    for(int i = 0; i < Num; i++){
      cout << *out <<endl;
    }
    return 0;
}
现在编译出现这样的问题
1.:-1: error: symbol(s) not found for architecture x86_64;
2::-1: error: linker command failed with exit code 1 (use -v to see invocation)
编译结果如下:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -headerpad_max_install_names -stdlib=libc++ -Wl,-syslibroot,/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk -mmacosx-version-min=10.7 -o class_template1 main.o stack.o   
Undefined symbols for architecture x86_64:
"Stack<char const*>::pop(char const*&)", referenced from:
      _main in main.o
"Stack<char const*>::push(char const* const&)", referenced from:
      _main in main.o
"Stack<char const*>::isFull()", referenced from:
      _main in main.o
"Stack<char const*>::isEmpty()", referenced from:
      _main in main.o
"Stack<char const*>::Stack(int)", referenced from:
      _main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** Error 1
10:09:02: 进程"/usr/bin/make"退出,退出代码 2 。
Error while building/deploying project class_template1 (kit: Desktop Qt 5.6.1 clang 64bit)
When executing step "Make"
请问这种错误该如何解决?

DarkSE 发表于 2016-9-19 11:19:32

你那个 模板类 类和实现不要分开文件写

BigSpoon 发表于 2016-9-19 12:14:19

Thanks

得意的屁 发表于 2016-9-19 20:31:48

楼主 这些代码是多久写完的 {:10_261:}

BigSpoon 发表于 2016-10-24 19:01:47

怎么?
页: [1]
查看完整版本: 关于类模板问题?