I'm trying to define my own Queue class, but the complier generates me the "ld, undefined symbols", which contains the constructor and reconstructor. However, I've put and wrote the definition and declaration in the codes. And also I build them together. So O don't know where am I wrong.
Thank you for helping me, I really don't know how to deal with it.
here's mt header:
#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__) */
Here's my 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;
}
And here's the main, just to test the Queue class.
#include <iostream>
#include "Queue.h"
int main()
{
Queue<int> test0 ;
//test0.push(10);
//std::cout << test0.size();
return 0;
}
The errs by Xcode are:
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)