这是我的代码:
// 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;
}
我从编译器中得到以下错误:
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 : '.'
编译器似乎不喜欢类对象定义之后的那一行。当我尝试调用 init() 成员时。我能想到的就是它与指向函数引用的指针有关。也许有人可以为我阐明这一点?
最佳答案
您可以在其他方法/函数之外直接调用调用函数/方法。
Scheduler.Init(Scheduler::Task &task_list,1);
这一行有2个问题。 以上似乎在任何功能/方法之外。例如。你可以放在 main 里面。
该行本身不正确。所以改成
scheduler.Init(&task_list,1);
通常你调用对象上的方法而不是类名,静态方法除外。将参数传递给方法时不传递参数类型。
所以 main 中更改的行看起来像
int _tmain(int argc, _TCHAR* argv[])
{
scheduler.Init(&task_list,1);
return 0;
}
关于c++ - 我在这里做错了什么?使用指向函数 typedef 的指针定义类。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17140494/