我正在使用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
}
我意识到我的代码的很大一部分仍然需要工作,但正是这个错误导致我无法测试我的代码。我迷失了造成这个错误的原因并花了相当多的时间。这个问题似乎影响了我从实现中调用的任何方法。关于问题是什么的任何想法?
答案 0 :(得分:1)
您可能需要显式实例化您的模板化类。您可以通过将以下行添加到包含您的实现的文件中来实现:
template class fsu::Stack<char, 10>;
这会强制编译器实例化该模板类的版本。
有关语法的更多信息:http://en.cppreference.com/w/cpp/language/class_template
(向下滚动到显式实例化。)