我正在尝试定义自己的Queue类,但编译器会生成“ld,undefined symbols”,其中包含构造函数和重构器。 但是,我已经在代码中写了定义和声明。我也一起建造它们。所以我不知道我错在哪里。
感谢您帮助我,我真的不知道如何处理它。
这是mt标题:
#ifndef __C4E2A1_Queue_Container__Queue__
#define __C4E2A1_Queue_Container__Queue__
#include <iostream>
#include <stdexcept>
#include <cassert>
template <typename T> class Queue
{
public:
Queue();
~Queue();
Queue(const Queue &);
Queue &operator=(const Queue &);
void push(const T &);
void pop();
T &front();
const T &front() const;
bool empty() const;
size_t size() const;
private:
T *v_;
T *vhead_; // for queue's head and tail FIFO
size_t vsize_;
size_t vused_; // once the place has been written, it's used
size_t vpopped_; // when pop from head of the queue, log it.
};
#endif /* defined(__C4E2A1_Queue_Container__Queue__) */
这是我的cpp
#include "Queue.h"
template <typename T>
Queue<T>::Queue()
: v_(0),
vsize_(10),
vhead_(0),
vused_(0), // nothing used
vpopped_(0) // nothing popped
{
v_ = new T[vsize_];
vhead_ = v_;
}
template <typename T>
Queue<T>::~Queue()
{
delete [] v_;
}
template <typename T>
T *
newCopy(const T *src, size_t srcsize, size_t destsize)
{
assert(destsize >= srcsize);
T *dest = new T[destsize];
try
{
copy(src, src + srcsize, dest);
}
catch (...)
{
delete [] dest;
throw ;
}
return dest;
}
template <typename T>
Queue<T>::Queue(const Queue &other)
: v_(newCopy(other.v_,
other.vsize_,
other.vsize_)),
vhead_(other.vhead_),
vsize_(other.vsize_),
vused_(other.vused_),
vpopped_(other.vpopped_)
{
}
template <typename T>
Queue<T> &
Queue<T>::operator=(const Queue<T> &other)
{
if (this != &other) {
T *v_new = newCopy(other.v_,
other.vsize_,
other.vsize_);
delete[] v_;
v_ = v_new;
vhead_ = other.vhead_;
vpopped_ = other.vpopped;
vsize_ = other.vsize_;
vused_ = other.vused_;
}
return *this;
}
template <typename T>
size_t
Queue<T>::size() const
{
return (vused_ - vpopped_);
}
template <typename T>
void
Queue<T>::push(const T& t)
{
if (vused_ == vsize_) {
size_t vsize_new = vsize_ * 2 + 1;
T *v_new = newCopy(vhead_, (vsize_ - vpopped_), vsize_new);
delete[] v_;
v_ = v_new;
vhead_ = v_new;
vpopped_ = 0;
vsize_ = vsize_new;
}
// put the elem into the queue
v_[vused_] = t;
++vused_;
// show where is the tail
//++vtail_;
}
template <typename T>
void
Queue<T>::pop()
{
if (vused_ == 0) {
throw std::logic_error("pop from empty queue");
}
else
{
// let the start of the queue to the next elem
++vhead_;
// pop this one out
++vpopped_;
}
}
template <typename T>
T &
Queue<T>::front()
{
if (vused_ == 0) {
throw std::logic_error("empty queue");
}
else
{
return vhead_;
}
}
template <typename T>
const T&
Queue<T>::front() const
{
if (vused_ == 0) {
throw std::logic_error("empty queue");
}
else
{
return *vhead_;
}
}
template <typename T>
bool
Queue<T>::empty() const
{
// if three elem come int and three get popped
// it's empty
if ((vused_ - vpopped_) == 0)
return true;
else
return false;
}
这是主要的,只是为了测试Queue类。
#include <iostream>
#include "Queue.h"
int main()
{
Queue<int> test0 ;
//test0.push(10);
//std::cout << test0.size();
return 0;
}
Xcode的错误是:
Undefined symbols for architecture x86_64:
"Queue<int>::Queue()", referenced from:
_main in main.o
"Queue<int>::~Queue()", 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)