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

时间:2011-12-04 23:41:35

标签: c++ compiler-errors

我是一名计算机科学专业的学生。我知道“指定冲突的返回类型”通常意味着你在声明之前使用了一个函数,但这个函数有点不同。由于严格的分配指南,我正在实现一个任务调度程序(我们自己的多线程程序),在一个名为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()’

我有什么想法可以让它停止发生?

1 个答案:

答案 0 :(得分:3)

问题在于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;
}