无效使用非静态成员函数-类成员函数调用另一个类成员函数

amanuel2

当我尝试编译时,我不断收到此消息:

task.c++:54:102: error: invalid use of non-static member function
      this->createTask(&otherTask, this->otherMain, mainTask.regs.eflags, (uint32_t*)mainTask.regs.cr3);

这是我的task.c ++函数:

#include "task.h"



static task_q *runningTask;
static task_q mainTask;
static task_q otherTask;
static PhyiscalMemoryManager *pmm_task;
static  Heap *heap_task;



extern void switch_task_a();

TaskManager::TaskManager(Heap *heap)
{
 heap_task = heap;
}
TaskManager::~TaskManager()
{}

Task::Task()
{}

Task::~Task()
{}
void TaskManager::otherMain()
{
        printf("Hello multitasking world!"); // Not implemented here...
        preempt();
}


void TaskManager::createTask(task_q* task, void(*task_main)(), uint32_t flags, uint32_t* pageDir)
{
        task->regs.ebx = 0;
        task->regs.ecx = 0;
        task->regs.edx = 0;
        task->regs.esi = 0;
        task->regs.edi = 0;
        task->regs.eflags = flags;
        task->regs.eip = (uint32_t) task_main;
        task->regs.cr3 = (uint32_t) pageDir;
        task->regs.esp = (uint32_t) (heap_task->k_malloc(TASK_STACK_SIZE)) + 0x1000; // Not implemented here
        task->next = 0;
}

void TaskManager::init_tasking()
{
     // Get EFLAGS and CR3
        __asm __volatile("movl %%cr3, %%eax; movl %%eax, %0;":"=m"(mainTask.regs.cr3)::"%eax");
        __asm __volatile("pushfl; movl (%%esp), %%eax; movl %%eax, %0; popfl;":"=m"(mainTask.regs.eflags)::"%eax");

        this->createTask(&otherTask, this->otherMain, mainTask.regs.eflags, (uint32_t*)mainTask.regs.cr3);
        mainTask.next = &otherTask;
        otherTask.next = &mainTask;

        runningTask = &mainTask;
}

void TaskManager::switchTask(Registers *old, Registers *new_)
{
    switch_task_a();
}

void TaskManager::preempt()
{
    task_q *last = runningTask;
    runningTask = runningTask->next;
    switchTask(&last->regs, &runningTask->regs);
}

这是我的任务。

#ifndef _TASK_H_
#define _TASK_H_ 1

#include <stdarg.h>
#include <stdint.h>
#include "gdt.h"
#include "stdio.h"
#include "heap.h"
#include "pmm.h"

#define TASK_STACK_SIZE 0x2000

typedef struct {
        uint32_t eax, ebx, ecx, edx, esi, edi,
                 esp, ebp, eip, eflags, cr3;
} Registers;

typedef struct task_q {
            Registers regs;
            struct task_q *next;
} task_q;

class Task
{
    friend class TaskManager;
public:
    Task();
    ~Task();
private:
};

class TaskManager
{
public:
    TaskManager(Heap *heap);
    ~TaskManager();
    void init_tasking();
    static void createTask(task_q* task, void(*task_main)(), uint32_t flags, uint32_t* pageDir);
    void preempt();
private:
    void switchTask(Registers *old, Registers *new_);
    void otherMain();
};

#endif

打电话this->OtherMain()到里面有什么问题this->createTask()吗?

用户名

void(*task_main)()期望有一个指向函数的指针。您尝试提供它void otherMain();是一个类方法并具有隐藏this参数。

这东西有些古怪。这是有关某些弊端以及如何避免它的精彩文章。

您将需要对如何执行此操作进行一些重新思考。您可以使用static void task_runner(void * userparam)方法(否this)和用户参数(可以通过静态方法进行强制转换)来提供TaskManager可调用的参数otherMain您可以打开void(*task_main)()进入void(TaskManager::*task_main)(),你仍然必须提供TaskManager调用的方法指针。

这真是一个令人讨厌的生意,但是我想让您感兴趣std::bind吗?

编辑

任务运行器与此类似:

class taskRunner
{
public:
    virtual execute() = 0;
    static void task_runner(void * userparam)
    {
        taskRunner* task = (taskRunner*)userparam;
        task->execute();
    }
};

缺点是您尝试运行的所有内容都必须是继承taskRunner和实现的类,execute并且必须跟踪userparam涉及了不小的努力,但是到底是什么。看起来您正在编写自己的OS。全方位的不懈努力。

它可能对您更好,它将抽象推到了一个更高的层次,并且只接受自由函数和静态方法。让正在运行的函数弄清它是否是一个类。

这意味着除非它是静态的otherMainTaskManager否则不能成为成员,这将需要对其进行重写,TaskManager以允许横切行为(例如任务的睡眠能力,产生其时间片和其他OS好东西)在不知道内部的情况下被调用TaskManager

otherMain 可能只是

void otherMain()
{
    printf("Hello multitasking world!"); 
    yield(); // a free function that calls into the system's TaskManager to preempt
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何从另一个函数调用属于类成员的函数?

另一个类的模板成员函数中的模板类中的调用方法

调用另一个类的成员函数的类设计

C++ - 使用指针调用另一个类的成员函数

如何使用另一个类的公共成员函数作为参数调用线程

在构造函数中从另一个成员构造一个类成员

让类成员函数在类外部调用一个函数

使用另一个没有对象的类的成员函数并避免静态

在另一个类中调用成员函数c ++

如何从另一个类调用成员指针函数?

无效使用非静态成员函数

无效使用非静态成员函数C ++

如何从另一个嵌套类中调用某个封闭类的嵌套类的函数指针成员的值?

您如何访问另一个类中一个类的成员函数?

在作为静态成员包含在另一个类中的类的构造函数中使用cout

如何修复导入另一个库的库中的“非静态成员函数的无效使用”

两个类将另一个类的成员函数作为朋友

想要一个静态成员函数来调用同一类的成员变量

我如何获得一个类成员函数以访问另一个类成员函数的私有成员?

从另一类调用一个类的成员函数

通过作为另一个类的成员函数的友元函数设置值

将成员函数地址传递给另一个类的函数

具有来自另一个类的成员函数的类中的线程

Python:将类成员函数传递给另一个类的回调

声明并使用指向另一个类中成员函数的指针

如何在另一个类构造函数中使用一个类的成员

C ++使用模板类调用非静态成员函数

另一个成员函数中的 Javascript 成员函数调用

从另一个成员函数的实现中调用成员函数