2017-01-08 59 views
-2

我是C++線程中的一個新手,我不知道如何去除我在Visual Studio中的錯誤。它給出2個錯誤,如下所示: 1)C2672:'std :: invoke'找不到匹配的重載函數。 2)C2893:無法專門的功能型 '未知類型的std ::調用(_Callable & &,_Typers & & ...)'在一個類的公共函數中使用多線程

這是我的代碼:

#include <iostream> 
#include <string> 
#include <thread> 
using namespace std; 
struct q_node { 
    q_node *previous; 
    q_node *next; 
    string data; 

}; 
class q { 
private: 
    q_node * aa; 
    q_node * bb; 
    q_node * ans; 
    q_node *front; 
    q_node *current; 
    int element; 
    int counter1; 
    int counter2; 
public: 
    q() { 
     front = NULL; 
     current = NULL; 
     element = 0; 
     aa = NULL; 
     bb = NULL; 
    } 
    void enque(string team) { 
     q_node *t = new q_node; 
     t->previous = NULL; 
     t->next = NULL; 
     t->data = team; 
     if ((front == NULL) && (current == NULL) && (element == 0)) { 
      front = t; 
     } 
     else { 
      current->next = t; 
      t->previous = current; 
     } 
     element++; 
     current = t; 
    } 
    void insert_at_index(int n, string x) { 
     q_node * t = new q_node; 
     t->previous = NULL; 
     t->next = NULL; 
     t->data = x; 
     q_node *temp, *temp2; 
     temp = front; 
     temp2 = temp->next; 
     if (n == 1) { 
      t->next = temp; 
      temp->previous = t; 
      front = t; 
     } 
     else { 
      for (int i = 0; i < element - 1; i++) { 
       if (i == (n - 2)) { 
        temp->next = t; 
        t->previous = temp; 
        t->next = temp2; 
        temp2->previous = t; 
        temp2 = front; 
       } 
       temp = temp->next; 
       temp2 = temp2->next; 
      } 

     } 
     element++; 
    } 

    void deque() { 
     q_node *temp = front; 
     front = front->next; 
     front->previous = NULL; 
     temp->next = NULL; 
     element--; 
     delete temp; 
    } 
    void display() { 
     q_node *t = front; 
     for (int i = 0; i < element; i++) { 
      cout << " " << t->data << " "; 
      t = t->next; 
     } 
    } 
    inline void checking() {         //decide values for both integer e.g for 9 (4,5) 
     if ((element % 2) == 0) { 
      counter1 = ((element/2) - 1); 
      counter2 = ((element/2) + 1); 
     } 
     else { 
      counter1 = ((element/2)); 
      counter2 = ((element/2) + 1); 
     } 
    } 
    void search_first(string x) {    //transverse from start 
     q_node *t = front; 
     int i = 0; 
     for (; i < counter1; i++) { 
      if (t->data == x) { 
       bb= t; 
       break; 
      } 
      t = t->next; 
     } 
     if (i == counter1) { 
      bb= NULL; 
     } 
    } 
    void search_last(string x) {     //transverse from end 
     q_node *t = current; 
     int i = 0; 
     for (; i < counter2; i++) { 
      if (t->data == x) { 
       aa= t; 
      } 
      t = t->previous; 
     } 
     if (i==counter2) { 
      aa = NULL; 
     } 
    } 
    void get_search(string aaa) { 

     thread t(&q::search_first, aaa); 
     thread tt(&q::search_last, aaa); 
     t.join(); 
     tt.join(); 
     if (aa != NULL&&bb != NULL) { 
      if (aa != NULL) { 
       ans = aa; 
      } 
      else { 
       ans = bb; 
      } 
     } 
     else { 
      ans = NULL; 
     } 
    } 
    void smart_insertion(string a) { 
     get_search(a); 
     if (ans!=NULL) { 
      q_node * t = new q_node; 
      t->data = a; 
      t->previous = ans; 
      t->next = (ans->next); 
      ans->next = t; 
      (ans->next)->previous = t; 
     } 
     else { 
      enque(a); 
     } 
    } 
}; 

我想使用2個線程從兩側讀取鏈接列表,以便提高總體速度。這裏的隊列使用雙向鏈表來實現。有人可以幫助我解決這些錯誤。

回答

0

要調用非靜態成員函數上的線程,您需要將它傳遞給將運行它的對象的指針。 this是:

thread t(&q::search_first, this, aaa); 
    thread tt(&q::search_last, this, aaa); 

我當然不答應你的代碼實現,你在做什麼,但是這將讓你過去的這個錯誤,代碼將編譯。

+0

非常感謝。它有幫助 – Asad

相關問題