【发布时间】:2013-06-17 04:38:55
【问题描述】:
这是我的代码:
// 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);不合法。不在命名空间范围内,不在类范围内... -
projects\ workdamnit\workdamnit\workdamnit.cpp,我喜欢它。
-
你试图从哪里调用“Init”,你试图从哪个对象实例调用它?
-
当你解决这个问题时(出于教育目的),使用
std::function<void(void)>:)
标签: c++ class function pointers typedef