错误:指定的返回类型冲突,与通常不同

error: conflicting return type specified, different than usual

本文关键字:常不同 冲突 返回类型 错误      更新时间:2023-10-16

我是计算机科学专业的学生。我知道"指定的返回类型冲突"通常意味着你在声明函数之前使用它,但这一个有点不同。由于严格的分配准则,我正在实现一个任务调度器(我们自己的多线程),在一个名为Task的类中,在Task.h中我们有:

void Task::Start(){
    int * returnval = new int;
    *returnval = pthread_create(&thread_id,NULL,tfunc,this);        
    delete returnval;
}

然后在另一个文件schedulable.h中,我们有:

int Schedulable::Start(){ 
    try{ 
        Task::Start();
        return 0; 
    }catch(int e) { return 1; } 
}

当我编译它时,我有一个"冲突的返回类型"错误:

In file included from scheduler.H:59, from task_test_step2.cpp:9: schedulable.H:162: error: conflicting return type specified for ‘virtual int Schedulable::Start()’ task.h:157: error: overriding ‘virtual void Task::Start()’

有什么办法能让我停止这种情况吗?

问题是Schedulable::Start覆盖Task::Start并将返回类型从void更改为int。您可能也想让Task::Start返回int:

int Task::Start(){
    // no need to use new here!
    int returnval = pthread_create(&thread_id,NULL,tfunc,this);        
    return returnval;
}