samedi 30 septembre 2017

Error when compiling multi-threaded C++ though my function arguments are fine

After reading several articles on SO about similar errors, I however was unsuccessful to understand the cause behind these errors. The error I get on compiling on VS 2015 I get are :

Severity Code Description Project File Line Suppression State Error C2893 Failed to specialize function template 'unknown-type std::invoke(_Callable &&,_Types &&...)' Busy_Waiting c:\program files (x86)\microsoft visual studio 14.0\vc\include\thr\xthread 240

Severity Code Description Project File Line Suppression State Error C2672 'std::invoke': no matching overloaded function found Busy_Waiting c:\program files (x86)\microsoft visual studio 14.0\vc\include\thr\xthread 240

#include "stdafx.h"
#include "iostream"
#include "thread"
using namespace std;


class Node
{
public:
    int value;
public:
    Node *next;
    Node(int val);
};
Node::Node(int val)
{
    value = val;
}

class tQueue {
public:
    Node *front;
    Node *rear;
    Node *prev;
public:
    ~tQueue();
    void enqueue(int val);
    Node* dequeue();
    void display();
};

void tQueue::enqueue(int val)
{
    Node* temp;
    temp = new Node(val);

    if (front == NULL)
    {
        front = temp;
        rear = temp;
        prev = temp;
    }
    else
    {
        prev = rear;
        rear->next = temp;
        rear = rear->next;

    }
}

Node* tQueue::dequeue() {
    prev = front;
    while (prev->next != rear)
    {
        prev = prev->next;
    }
    if (rear != NULL) {
        rear = prev;
        delete rear->next;
        rear->next = NULL;
    }
    return rear;
}

void tQueue::display() {
    Node* iter = front;
    while (iter != NULL) {
        cout << iter->value;
        iter = iter->next;
    }
}

tQueue::~tQueue() {
    delete this;
}

int main() 
{
    //tQueue<int> obj ;

    std::thread t1(&tQueue::enqueue, 1);
    std::thread t2(&tQueue::enqueue, 2);
    t1.join();
    t2.join();

    return 0;
}

Aucun commentaire:

Enregistrer un commentaire