The problem is that you cannot run code in the global scope. The global scope is only for declarations or definitions, not for expressions (executing code). You have to move your setting for the Printed functor in the main function:
#include <iostream>
#include <string>
#include "events.h"
using namespace std;
class test
{
public:
events<> Printed{[]() { ; }};
void write(string a)
{
cout << a;
Printed();
}
};
int main()
{
test a;
a.Printed = events<>{[]()
{
cout << "\nMessage printed\n";
}};
a.write("hello world");
cin.get();
return 0;
}
I think this should work, although I have no idea how "events" is set up.