我正在处理的一些同意多任务代码的构造函数和析构函数有问题。我收到的错误是
task.o: In function `$_4Task':
/home/luke/project/task.cc(.text+0x57): undefined reference to `Task virtual table'
task.o: In function `_4TaskUi':
/home/luke/project/task.cc(.text+0x5): undefined reference to `Task virtual table'
从目标文件来看,_4Task 和 _4TaskUi 的取消引用名称分别是我的 Task 析构函数和构造函数。我似乎无法弄清楚为什么我的编译器告诉我我没有引用虚拟表。任何帮助将不胜感激,我在 ubuntu 12.10 上使用 GCC
以下是我的头文件
#ifndef TASK
#define TASK
#include "stddef.h"
#include "nocopy.h"
class Task: private NoCopy
{
void** stack; //Base of stack
void** sp; //saved sp when not running
static Task* current; //Point to running task
static void start(); //calls task::main
static void dispatch(Task* t); //switch context to task t
virtual void main();
friend class TaskList;
protected:
void fork(); //start task
public:
Task(size_t stackSize);
virtual ~Task();
static Task* const getCurrent()
{
return current;
}
static void yield();
};
#endif
以下是我的 cpp 文件
#include "task.h"
Task initialTask(0); //initial code before
// set up stack in crt0.s
Task* Task::current = &initialTask; //note first running task
TaskList readyList;
Task::Task(size_t stackSize)
:stack(new (void*)[stackSize/sizeof(void*)]),
sp(&stack[stackSize/sizeof(void*)])
{ //set up task stack
if(stackSize)
{
*--sp = 0; //for debugger
*--sp = 0; //terminate frame chain
*--sp = &start; //point to first code
}
}
Task::~Task()
{
delete[](stack);
}
//Contec Switching
register void** spreg asm("s"); //can refer to hc12 SP as spreg
void Task::dispatch(Task* task)
{
current -> sp = spreg;
current = task;
spreg = current -> sp;
}//Dispatch is called by one task but is returned by another
void Task::fork() //call in ctor of all task
{
TaskList::Node node(&readyList); //Make caller go from "running"->"ready"
dispatch(this); //Start new task
}
void Task::start()
{
current -> main();
TaskList forever; //Wait Task
forever.enqueue(); //Wait forever
}
编辑我想通了,因为每个人都说 main 没有在 task.h 或 task.cc 中定义,main 取决于任务,所以 main 的默认定义是
virtual void main(){};