简体   繁体   中英

What am i doing wrong here? Defining a class with a pointer to function typedef.

Here is my code:

// WorkDamnit.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


class Scheduler
{

public:

    typedef void (*function_ptr) (void); 

    struct Task
    {
        function_ptr    function; 
        int             numOfTasks;
    };

    void Init(Task *tasks, int numOfTasks); 



private: 
    int     _numOfTasks; 
    Task    *_tasks; 

};

void Scheduler::Init(Scheduler::Task *tasks, int numOfTasks)
{
    _tasks = tasks; 
    _numOfTasks = numOfTasks;
}


void count() {}; 



Scheduler::Task task_list = 
{
    count, 1
}; 


Scheduler scheduler; 

Scheduler.Init(Scheduler::Task &task_list,1); 

int _tmain(int argc, _TCHAR* argv[])
{
    return 0;
}

I get the following errors from the compiler:

1>c:\users\evan\documents\visual studio 2012\projects\workdamnit\workdamnit\workdamnit.cpp(49): error C2143: syntax error : missing ';' before '.'
1>c:\users\evan\documents\visual studio 2012\projects\workdamnit\workdamnit\workdamnit.cpp(49): error C2059: syntax error : '.'

The compiler doesnt seem to like the line after the class object definition. When i try to call the init() member. All i can think of is that it has to do with the pointer to function reference. Maybe someone can shed some light on this for me?

You can call call functions/methods directly outside of other methods/functions.

Scheduler.Init(Scheduler::Task &task_list,1);

2 problems in this line. The above seems to be outside of any function/method. For eg. you can put in inside main.

The line itself is not correct. So change it to

scheduler.Init(&task_list,1);

Usually you call a method on an object not a class name, except for static methods. You don't pass the parameter type while passing parameters to the method.

So the changed line in main will look like

int _tmain(int argc, _TCHAR* argv[])

{
    scheduler.Init(&task_list,1); 

    return 0;
}

Line 49 should be:

scheduler.Init(Scheduler::Task &task_list,1); // note the lowercase 's': the object should be used, not the class

Also it should be within a function (maybe main in your case)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM