[RESOLVED] template for events - Page 2
 CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

# Thread: [RESOLVED] template for events

1. Member +
Join Date
Apr 2009
Posts
660

## Re: template for events

now i'm testing inside of a class, but i get several errors
Code:
//events.h
#ifndef events_H_INCLUDED
#define events_H_INCLUDED

#include <functional>
#include <vector>

template <typename ... b>
class events
{
public:
typedef std::function<void(b...argx )> OnSomethingHandler;

events(OnSomethingHandler Handler)
{
handlers_=Handler;
}

void operator ()(b... args)
{
handlers_(args...);
}
events& operator = (OnSomethingHandler Handler)
{
handlers_ = Handler;
return *this;
}

private:
OnSomethingHandler handlers_;

};

#endif // events_H_INCLUDED
and how i use it:

Code:
#include <iostream>
#include "events.h"

using namespace std;

class test
{
private:
int x=20;
int y=0;
public:
events<int, int> created;
events<int, int> move;

test()
{
created([](int a, int b) { ; });
created(x,y);

}

void setX(int s)
{
x=s;
move=([](int a, int b) { ; });
move(x,y);
}
};

int main()
{
test a;
a.created=[](int a, int b)
{

if (b==0)
cout << "you can't divide by zero";
else
cout << (a/b);

};
a.created(8,2);
a.Move=[](int a)
{
if (a<0)
cout << "hide";
else
cout << "show";
}
a.setX(10);
cin.get();
}
"what i need is the class call the events object and pass to it same values(depending on object class members).
but seems that i get several errors:
like declaring 2 events
heres the errors messages:
C:\Users\Joaquim\Documents\CodeBlocks\My Class\main.cpp||In constructor 'test::test()':|
C:\Users\Joaquim\Documents\CodeBlocks\My Class\main.cpp|16|error: no matching function for call to 'events<int, int>::events()'|
C:\Users\Joaquim\Documents\CodeBlocks\My Class\main.cpp|16|note: candidates are:|
C:\Users\Joaquim\Documents\CodeBlocks\My Class\events.h|13|note: events<b>::events(events<b>::OnSomethingHandler) [with b = {int, int}; events<b>::OnSomethingHandler = std::function<void(int, int)>]|
C:\Users\Joaquim\Documents\CodeBlocks\My Class\events.h|13|note: candidate expects 1 argument, 0 provided|
C:\Users\Joaquim\Documents\CodeBlocks\My Class\events.h|8|note: events<int, int>::events(const events<int, int>&)|
C:\Users\Joaquim\Documents\CodeBlocks\My Class\events.h|8|note: candidate expects 1 argument, 0 provided|
C:\Users\Joaquim\Documents\CodeBlocks\My Class\events.h|8|note: events<int, int>::events(events<int, int>&&)|
C:\Users\Joaquim\Documents\CodeBlocks\My Class\events.h|8|note: candidate expects 1 argument, 0 provided|
C:\Users\Joaquim\Documents\CodeBlocks\My Class\main.cpp|16|error: no matching function for call to 'events<int, int>::events()'|
C:\Users\Joaquim\Documents\CodeBlocks\My Class\main.cpp|16|note: candidates are:|
C:\Users\Joaquim\Documents\CodeBlocks\My Class\events.h|13|note: events<b>::events(events<b>::OnSomethingHandler) [with b = {int, int}; events<b>::OnSomethingHandler = std::function<void(int, int)>]|
C:\Users\Joaquim\Documents\CodeBlocks\My Class\events.h|13|note: candidate expects 1 argument, 0 provided|
C:\Users\Joaquim\Documents\CodeBlocks\My Class\events.h|8|note: events<int, int>::events(const events<int, int>&)|
C:\Users\Joaquim\Documents\CodeBlocks\My Class\events.h|8|note: candidate expects 1 argument, 0 provided|
C:\Users\Joaquim\Documents\CodeBlocks\My Class\events.h|8|note: events<int, int>::events(events<int, int>&&)|
C:\Users\Joaquim\Documents\CodeBlocks\My Class\events.h|8|note: candidate expects 1 argument, 0 provided|
C:\Users\Joaquim\Documents\CodeBlocks\My Class\main.cpp|17|error: no match for call to '(events<int, int>) (test::test()::__lambda0)'|
C:\Users\Joaquim\Documents\CodeBlocks\My Class\events.h|8|note: candidate is:|
C:\Users\Joaquim\Documents\CodeBlocks\My Class\events.h|18|note: void events<b>:perator()(b ...) [with b = {int, int}]|
C:\Users\Joaquim\Documents\CodeBlocks\My Class\events.h|18|note: candidate expects 2 arguments, 1 provided|
C:\Users\Joaquim\Documents\CodeBlocks\My Class\main.cpp||In function 'int main()':|
C:\Users\Joaquim\Documents\CodeBlocks\My Class\main.cpp|43|error: 'class test' has no member named 'Move'|
C:\Users\Joaquim\Documents\CodeBlocks\My Class\main.cpp|50|error: expected ';' before 'a'|
||=== Build finished: 5 errors, 0 warnings (0 minutes, 6 seconds) ===|"
what i'm doing wrong?

2. Elite Member Power Poster
Join Date
Apr 1999
Posts
27,431

## Re: template for events

Originally Posted by Cambalinho
now i'm testing inside of a class, but i get several errors
This is exactly what happens when you don't learn basic C++, and instead want to use advanced syntax without a proper track of learning the C++ language.

Look at this example:
Code:
class foo
{
public:
foo(int x);
};

class someClass
{
foo m_foo;
};

int main()
{
someClass s;
}
There is an error in the code. Can you fix the compile error without changing class foo? The hint is that the error with the code above and the error you have are the same.

Also, note that this is basic C++ knowledge. A beginner in the language must know how to fix that error -- it is not advanced, doesn't use lamdas, templates, or any of that new C++ 11 stuff you're throwing around.

Regards,

Paul McKenzie

3. Member +
Join Date
Apr 2009
Posts
660

## Re: template for events

Originally Posted by Paul McKenzie
This is exactly what happens when you don't learn basic C++, and instead want to use advanced syntax without a proper track of learning the C++ language.

Look at this example:
Code:
class foo
{
public:
foo(int x);
};

class someClass
{
foo m_foo;
};

int main()
{
someClass s;
}
There is an error in the code. Can you fix the compile error without changing class foo? The hint is that the error with the code above and the error you have are the same.

Also, note that this is basic C++ knowledge. A beginner in the language must know how to fix that error -- it is not advanced, doesn't use lamdas, templates, or any of that new C++ 11 stuff you're throwing around.

Regards,

Paul McKenzie
the class foo have 1 construtor witth 1 argument. so the
Code:
 foo m_foo;
must be:
Code:
 foo m_foo(3);
or with a int variable.
thanks for that thot
my problem was that and copy from 1 person that help me, and i don't see that point

i more or less know. but why the created() isn't showed?
Code:
#include <iostream>
#include "events.h"

using namespace std;

class test
{
int x=20;
int y=50;
public:
events<> created{[]() { ; }};
events<int, int> moved{[](int a, int b) { ; }};

test()
{
created();
}

void setx(int s)
{
x=s;
moved(x,y);
}
};

int main()
{
test a;
a.created=[]()
{
cout << "hello world";
};
a.moved=[](int a, int b)
{
cout << "moved to: x = " << a << " y = " << b;

};
a.setx(100);

cin.get();
}
it's because i changed the created() after i create 'a'?

4. Elite Member Power Poster
Join Date
Apr 1999
Posts
27,431

## Re: template for events

Originally Posted by Cambalinho
the class foo have 1 construtor witth 1 argument. so the
Code:
 foo m_foo;
must be:
Code:
 foo m_foo(3);
or with a int variable.
thanks for that thot
That does not fix the error. To prove it doesn't fix it, repost my code with your "fix".

The foo is a member variable of the someClass class. How do you initialize member variables that do not have default initializations? This is a basic C++ question, and all beginners with a couple of weeks to a month of C++ knowledge must be able to answer that question without hesitation.

You are missing a basic fundamental of C++ and classes, but in your haste to write "cool" code, you miss these basics. In your changed code, what you did was to change your code in a way that you never learn these basics.

Again, take my code, do not change foo, and do not make m_foo into something it isn't. It is a non-static member of the someClass class, and it must stay that way. So now, how do you fix the code I posted? And again, it is the same error (and fix) you must make to your original example to get past the compiler error.

Regards,

Paul McKenzie
Last edited by Paul McKenzie; October 17th, 2013 at 11:00 AM.

5. ## Re: template for events

must be:
Code:
 foo m_foo(3);
The c++ language stipulates that 'only const static integral data members can be initialized inside a class or struct'. This statement violates all three! So your suggested solution is not correct. As Paul states in post #17, anyone who is programming with c++ classes must be able to easily fix this problem. It is not complicated, it just requires some basic understanding of classes.

If you don't know how to do this, then I strongly suggest that you take some time out from writing your 'clever' code and learn the basics of the c++ language. You cannot 'guess' with c++. You have to know and understand which requires learning. Doing this may not be as 'interesting' as writing template code using the new features of c++11 but the effort put into it will repay you later.

6. Member +
Join Date
Apr 2009
Posts
660

## Re: template for events

after several times(to be honest), fix the error:
Code:
#include <iostream>

using namespace std;

class foo
{
public:
foo(int x);
};

class someClass
{
public:
foo m_foo;

};

int main()
{
someClass s();
}
but why the '()' on 's', it's because the foo construtor isn't used(just declared)?

7. Member +
Join Date
Apr 2009
Posts
660

## Re: template for events

Originally Posted by 2kaud
The c++ language stipulates that 'only const static integral data members can be initialized inside a class or struct'. This statement violates all three! So your suggested solution is not correct. As Paul states in post #17, anyone who is programming with c++ classes must be able to easily fix this problem. It is not complicated, it just requires some basic understanding of classes.

If you don't know how to do this, then I strongly suggest that you take some time out from writing your 'clever' code and learn the basics of the c++ language. You cannot 'guess' with c++. You have to know and understand which requires learning. Doing this may not be as 'interesting' as writing template code using the new features of c++11 but the effort put into it will repay you later.
it can be easy, but like i said. i take several times to do it. and i don't understand why
anotherthing(to both), how can i do it, for the test construtor accept my function change?

8. Member +
Join Date
Apr 2009
Posts
660

## Re: template for events

after some work and read more, i fix my code:
Code:
#include <iostream>
#include "events.h"

using namespace std;

class test
{
int x=20;
int y=50;
public:

events<int, int> moved{[](int a, int b) { ; }};
;
test(std::function<void(void)> Created)//heres a parameter for a function\lambda
{
Created();
}

void setx(int s)
{
x=s;
moved(x,y);
}
};

int main()
{
test a([]()
{
cout << "hello world" << endl;
});//like you see i don't use ';' after '}'

a.moved=[](int a, int b)
{
cout << "moved to: x = " << a << " y = " << b;

};
a.setx(100);

cin.get();
}
now the Created event is called
thanks for all to all

9. Elite Member Power Poster
Join Date
Apr 1999
Posts
27,431

## Re: template for events

Originally Posted by Cambalinho
the class foo have 1 construtor witth 1 argument. so the
Code:
 foo m_foo;
must be:
Code:
 foo m_foo(3);
Another hint -- the fix requires something that is fundamental, but for some reason, you never use this fundamental of C++ in any of the code you post (either in this thread or other threads that you've posted code in).

Regards,

Paul McKenzie
Last edited by Paul McKenzie; October 17th, 2013 at 11:32 AM.

10. Elite Member Power Poster
Join Date
Apr 1999
Posts
27,431

## Re: template for events

Originally Posted by Cambalinho
it can be easy, but like i said. i take several times to do it. and i don't understand why
anotherthing(to both), how can i do it, for the test construtor accept my function change?
I wish all C++ programmers can take a look at this thread. This thread serves as an example of what happens when you just use C++ without properly learning it.

The reason why I am hesitant in giving the answer is because it's time you have to show some effort on your part to actually learn the language. It gets frustrating if you post code using advanced features, and then the reason for the problem(s) that you have is that you didn't learn basic fundamentals of the language.

I gave a simple example -- forget about your code for one minute. I would like you to fix my example program, keeping m_foo as a member, and not changing the foo class.

Regards,

Paul McKenzie

11. Elite Member Power Poster
Join Date
Apr 1999
Posts
27,431

## Re: template for events

Originally Posted by Cambalinho
after some work and read more, i fix my code:
And you still haven't learned why your original code didn't work.

Again, all you did was change your code to fit what you know, completely missing the fundamental of why your original code didn't work. Myself plus others here know how to fix your original code without rearranging anything. If you want to learn C++, take your original code (or my code), and fix it without rearranging or changing the code structure.

Regards,

Paul McKenzie

12. Elite Member Power Poster
Join Date
Apr 1999
Posts
27,431

## Re: template for events

Originally Posted by Cambalinho
after several times(to be honest), fix the error:
Code:
#include <iostream>

using namespace std;

class foo
{
public:
foo(int x);
};

class someClass
{
public:
foo m_foo;

};

int main()
{
someClass s();
}
The fix is not in the main() function. Another hint -- the fix is in the someClass class. It is missing something that is required to initialize the m_foo member. Note that I bolded initialize. Does that give you a clue?

Regards,

Paul McKenzie

13. Member +
Join Date
Apr 2009
Posts
660

## Re: template for events

Originally Posted by Paul McKenzie
The fix is not in the main() function. Another hint -- the fix is in the someClass class. It is missing something that is required to initialize the m_foo member. Note that I bolded initialize. Does that give you a clue?

Regards,

Paul McKenzie
do you mean the construtor?

14. Elite Member Power Poster
Join Date
Apr 1999
Posts
27,431

## Re: template for events

Originally Posted by Cambalinho
do you mean the construtor?
Yes, but not only the constructor. There is still one item, in addition to the constructor, that needs to be specified to initialize the member (again, the keyword is initialize).

Regards,

Paul McKenzie

15. Member +
Join Date
Apr 2009
Posts
660

## Re: template for events

Originally Posted by Paul McKenzie
Yes, but not only the constructor. There is still one item, in addition to the constructor, that needs to be specified to initialize the member (again, the keyword is initialize).

Regards,

Paul McKenzie
with 1 parameter. but i still confuse
how we can initializate the m_foo?

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

This is a CodeGuru survey question.

Featured