Lpaulson
Lpaulson

Reputation: 219

Undefined reference to virtual table, Constructor and Destructor

I'm having trouble with the constructor and destructor of some consent multitasking code I'm working on. The error I'm receiving is

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'

from looking at the object file, it looks like the dereference names of _4Task and _4TaskUi are my Task destructor and constructor respectively. I cant seem to figure out why my compiler is telling me I have not referenced the virtual table. Any help would be greatly appreciated, I'm using using GCC on ubuntu 12.10

the following is my header file

#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

and the following is my cpp file

#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  
}

EDIT I figured it out, as every one was saying main was not defined in either task.h or task.cc, main is is dependent on the task so the default definition for main would be

virtual void main(){};  

Upvotes: 2

Views: 2764

Answers (1)

Alok Save
Alok Save

Reputation: 206518

You need to provide definitions for all virtual functions in your class. Only pure virtual functions can exist without a definition. The code you show does not have any definition for the function Task::main(). You need to define it.
On a second thought, I would name that function something more appropriate.


Good Read:

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

Upvotes: 4

Related Questions