未定义的对虚拟表,构造函数和析构函数的引用

时间:2013-03-31 07:18:01

标签: c++ polymorphism multitasking

我正在使用我正在处理的一些同意多任务代码的构造函数和析构函数。我收到的错误是

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  
}

编辑我想通了,因为每个人都说主要没有在task.h或task.cc中定义,main是依赖于任务所以main的默认定义是

virtual void main(){};  

1 个答案:

答案 0 :(得分:3)

您需要为班级中的所有virtual函数提供定义。只有纯虚函数才能存在而没有定义。您显示的代码没有函数Task::main()的任何定义。你需要定义它。
再想一想,我会说这个功能更合适。


好读:

<强> What does it mean that the "virtual table" is an unresolved external?