0

There is a problem with this code and I can't understand it. It seems the problem happens within the linking phase as I've googled, but in my case I think there is something wrong with my code, not in toolchains, but I just couldn't solve it after hours and hours of trying. I'd be happy if you help me correcting it. I'm using Code::Blocks, and the latest version of MinGW.

main.cpp

#include <iostream>
#include <string>
#include "threadsafequeue.hpp"
using namespace std;

int main(){
    threadsafe_queue<string> Q;
    threadsafe_queue<string> X;
    X.empty();
    try{
        string s;
    }catch(empty_queue ex){
        cout << ex.what() << endl;
    }
    return 0;
}

threadsafe_queue.cpp

#include "threadsafe_queue.hpp"

template <typename T>
threadsafe_queue<T>::threadsafe_queue(const threadsafe_queue& other){
    std::lock_guard<std::mutex> lock(other.m);
    threadsafe_queue<T>::data = other.data;
}

template <typename T>
void threadsafe_queue<T>::push(T new_value){
    std::lock_guard<std::mutex> lock(threadsafe_queue<T>::m);
    threadsafe_queue::data.push(new_value);
}

template <typename T>
std::shared_ptr<T> threadsafe_queue<T>::pop(){
    std::lock_guard<std::mutex> lock(threadsafe_queue<T>::m);
    if(data.empty()) throw empty_queue();
    std::shared_ptr<T> const res(std::make_shared<T>(threadsafe_queue<T>::data.front()));
    threadsafe_queue<T>::data.pop();
    return res;
}

template <typename T>
void threadsafe_queue<T>::pop(T& value){
    std::lock_guard<std::mutex> lock(threadsafe_queue::m);
    if(data.empty()) throw empty_queue();
    value = threadsafe_queue::data.front();
    threadsafe_queue::data.pop();
}

template <typename T>
bool threadsafe_queue<T>::empty(){
    std::lock_guard<std::mutex> lock(threadsafe_queue<T>::m);
    return threadsafe_queue<T>::data.empty();
}

threadsafe_queue.hpp

#include <exception>
#include <memory>
#include <mutex>
#include <queue>

#ifndef THREADSAFE_QUEUE_HPP_INCLUDED
#define THREADSAFE_QUEUE_HPP_INCLUDED

struct empty_queue : std::exception
{
    virtual const char * what() const throw()
    {
        return "The Queue is Empty.";
    };
};

template <typename T>
class threadsafe_queue
{
private:
    std::queue<T> data;
    mutable std::mutex m;
public:
    threadsafe_queue() {};
    threadsafe_queue(const threadsafe_queue& other);

    threadsafe_queue& operator= (const threadsafe_queue&) = delete;

    void push(T new_value);

    std::shared_ptr<T> pop();

    void pop(T& value);

    bool empty();
};

#endif // THREADSAFE_QUEUE_HPP_INCLUDED

And the error is :

...\ThreadSafeQueue\main.cpp|9|undefined reference to `threadsafe_queue::empty()'|

Ahmad Siavosh
  • 646
  • 1
  • 9
  • 22

2 Answers2

4

Move everything from threadsafe_queue.cpp to threadsafe_queue.hpp.

Why can templates only be implemented in the header file?

Or, if you want declaration and implementation of template to be separated, rename threadsafe_queue.cpp to threadsafe_queue.tpp (not really needed, just for better clarity) and include it at the end of threadsafe_queue.hpp file.

Community
  • 1
  • 1
Messa
  • 24,321
  • 6
  • 68
  • 92
1

You must include in your main.cpp also your implementation file:

threadsafe_queue.cpp

actually your template definitons must be in your threadsafe_queue.hpp file otherwise templates will not be instantiated.

marcinj
  • 48,511
  • 9
  • 79
  • 100