大家好我正在研究一个Thread类和一个CountingThread类,它来自Thread类,包括使用库的同步计数器。但是在创建这个CountingThread类时,我遇到了“不允许不完整类型”的问题所以如果你在一个糟糕的结构中形成这个Thread抽象类或者说我做错了什么的话你会给我一些建议我会很高兴的。 (仅供参考,我必须保留课程和方法,因为这是一项任务)
#ifndef _THREAD_H_
#define _THREAD_H_
#include <Windows.h>
#include <iosfwd>
class Thread{
private:
HANDLE hThread;
int idThread;
public:
Thread(LPTHREAD_START_ROUTINE fnct){ // here I'm trying to get a function and create thread with it
hThread = CreateThread(NULL, 0,fnct,NULL,0,(LPDWORD)&idThread);
}
virtual void main()=0;
void suspend(){
SuspendThread(hThread);
}
void resume(){
ResumeThread(hThread);
}
void terminate(){
TerminateThread(hThread,0);
}
static void sleep(int sec){
Sleep(sec*1000);
}
};
#endif
CountingThread.h
#ifndef _COUNTINGTHREAD_H_
#define _COUNTINGTHREAD_H_
#include "SynchronizedCounter.h"
#include "Thread.h"
class CountingThread :public Thread{
private:
SynchronizedCounter counter;
public:
CountingThread(counter.increment()){ // here I'm having the error "incomplete type on counter"
} // I want to create thread with the counter.increment function
};
#endif
SynchronizedCounter.h
#ifndef SYNCHRONIZEDCOUNTER_H_
#define SYNCHRONIZEDCOUNTER_H_
#include "Mutex.h"
#include <iosfwd>
class SynchronizedCounter{
private:
int count;
public:
SynchronizedCounter();
SynchronizedCounter(int);
void increment();
int value();
friend std::ostream &operator <<(std::ostream& output, const SynchronizedCounter& counter)
{
output << counter.count << endl;
return output;
}
};
#endif
和synchronizedCounter :: increment
void SynchronizedCounter::increment(){
Mutex mut;
mut.lock;
count++;
mut.unlock;
}
答案 0 :(得分:2)
似乎是语法错误。你应该在这里定义一个参数:
所以它应该是:
class CountingThread :public Thread{
private:
SynchronizedCounter counter;
public:
CountingThread()
{
counter.increment())
//... etc
} // I want to create thread with the counter.increment function
//...
};
无论如何,当counter.increment()返回void时,你不能将它作为参数传递。