我正在使用Xcode并在编译时收到以下错误消息:

Undefined symbols for architecture x86_64:
"fsu::Stack<char, 10ul>::Pop()", referenced from:
  _main in main.o
"fsu::Stack<char, 10ul>::Dump(std::__1::basic_ostream<char, std::__1::char_traits<char>      >&)", referenced from:
  _main in main.o
"fsu::Stack<char, 10ul>::Push(char const&)", referenced from:
  _main in main.o
"fsu::Stack<char, 10ul>::Stack(char)", referenced from:
  _main in main.o
"fsu::Stack<char, 10ul>::~Stack()", referenced from:
  _main in main.o
"fsu::Stack<char, 10ul>::Display(std::__1::basic_ostream<char, std::__1::char_traits<char>  >&, char) const", referenced from:
  _main in main.o
ld: symbol(s) not found for architecture x86_64


我的测试客户如下:

#include <iostream>
#include "stack.h"

int main()
{
  typedef char ElementType;
  ElementType fill = '*';
  const size_t maxSize = 10;
  typedef fsu::Stack < ElementType , maxSize > StackType;
  StackType s(fill);

  s.Push('a');
  s.Push('b');
  s.Pop();

  s.Display(std::cout, '\0');
  s.Dump(std::cout);

  return 0;
} // end main


我的头文件:

#include <iostream>

namespace fsu
{
  template < typename T, size_t N = 100>
  class Stack
  {
  public:
    // Stack < T , N > API
    void     Push     (const T& t); // push t onto stack; error if full
    T        Pop      ();           // pop stack and return removed element; error
                                    //if stack is empty
    T&       Top      ();           // return top element of stack; error if empty
    const T& Top      () const;     // const version
    size_t   Size     () const;     // return number of elements in stack
    size_t   Capacity () const;     // return storage capacity [maximum size] of
                                    // stack
    bool     Empty    () const;     // return 1/true if stack is empty, 0/false if
                                    //not empty
    void     Clear    ();           // make the stack empty
    void     Display  (std::ostream& os, char ofc = '\0') const; // output stack
                                    // contents through os, top to bottom
    void     Dump     (std::ostream& os); // output all private data (for dev only)
    Stack             ();           // default constructor
    Stack             (T fill);     // puts "fill" in each slot of the underlying
                                    // array (keeps size = 0)
    Stack             (const Stack&); // copy constructor
    ~Stack            ();             // destructor
    Stack& operator = (const Stack&); // assignment operator

  private:
    const size_t capacity_;  // = N = size of array   - fixed during life of stack
    T            data_[N];   // array of T objects    - where T objects are stored
    size_t       size_;      // current size of stack - dynamic during life of stack
  };
}


为了简洁起见,在这里我将仅包含构造函数和析构函数的实现:

#include "stack.h"
#include <iostream>

// Stack < T , N > API

template < typename T , size_t N >
fsu::Stack<T,N>::Stack () : capacity_(N), data_(), size_(0)
{}

template < typename T , size_t N >
fsu::Stack<T,N>::Stack (T fill) : capacity_(N), size_(0)       // puts "fill" in each slot       of the underlying array (keeps size = 0)
{
  for(size_t i = 0; i < capacity_; ++i)
    data_[i] = fill;
}

template < typename T , size_t N >
fsu::Stack<T,N>::~Stack ()              // empty destructor
{
  //nothing here
}


我意识到我的代码中仍有很大一部分仍然需要工作,但是正是这个错误使我无法测试我的代码。我对导致此错误的原因一无所知,并花费了大量时间。这个问题似乎正在影响我从实现中调用的任何方法。关于问题是什么?

最佳答案

您可能需要显式实例化模板化的类。您可以通过在实现中向文件添加以下行来做到这一点:

    template class fsu::Stack<char, 10>;


这将迫使编译器实例化该模板类的版本。

有关语法的更多信息,请参见:http://en.cppreference.com/w/cpp/language/class_template
(向下滚动到“显式实例化”。)

关于c++ - 体系结构x86_64的 undefined symbol :构建堆栈,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20415134/

10-11 21:35