Perform function with time interval SDL/c++ - c++

I want a function to be called every third second. In objective-c I would do like this:
NSTimer timer = [NSTimer timerWithInterval:3 sel:#selector(my_function)...];
How do I do this with SDL and c++? I've tried this:
SDL_TimerID myTimer = SDL_AddTimer(3,my_function(), NULL);
But I just get an error which says: No matching function for call to SDL_AddTimer.
What is wrong?

The first parameter should be 3000 since the documentation lists the units as milliseconds.
edit:
If the prototype doesn't match I get:
~/sdl$ g++ example.cpp -lSDL example.cpp: In function ‘int main(int,
char**)’: example.cpp:13:59: error: invalid conversion from ‘void
(*)()’ to ‘SDL_NewTimerCallback {aka unsigned int (*)(unsigned int,
void*)}’ [-fpermissive] /usr/include/SDL/SDL_timer.h:109:37: error:
initializing argument 2 of ‘_SDL_TimerID* SDL_AddTimer(Uint32,
SDL_NewTimerCallback, void*)’ [-fpermissive]
Which shows that the function prototype has to be:
unsigned int my_function(unsigned int, void * )
This compiles:
#include <SDL/SDL.h>
unsigned int my_function(unsigned int interval, void * param)
{
return interval; // repeat the timer
}
int main(int argc, char ** argv)
{
SDL_TimerID myTimer = SDL_AddTimer(3000, my_function, NULL);
return 0;
}

Related

explicit conversion to std::function

I'm trying to define explicit conversion from some class to std::function like this:
#include <functional>
class ExpInt { private:
const int value;
public:
ExpInt(const int v):value(v){}
explicit operator std::function<int (void)> ()
{
return [=](void){ return value; };
}
};
int main(int argc, char **argv)
{
auto e = new ExpInt(44);
auto f = static_cast<std::function<int (void)>>(e);
return 0;
}
But when I compile it I get the following error:
$ g++ main.cpp -o main
main.cpp: In function ‘int main(int, char**)’:
main.cpp:16:51: error: no matching function for call to ‘std::function<int()>::function(ExpInt*&)’
auto f = static_cast<std::function<int (void)>>(e);
^
The compiler tells you what's wrong:
error: no matching function for call to ‘std::function<int()>::function(ExpInt*&)’
auto f = static_cast<std::function<int (void)>>(e);
^
A pointer to ExpInt is not convertible to std::function<int (void)>. ExpInt would be convertible, so if you simply indirect through the pointer, that would work:
auto f = static_cast<std::function<int (void)>>(*e);
P.S. You leak the dynamic allocation. Avoid using owning bare pointers.

BOOST_STRONG_TYPEDEF, new expression, ambiguous default type conversion

I would think this should work:
#include "boost/serialization/strong_typedef.hpp"
BOOST_STRONG_TYPEDEF(int, StrongInt);
int main()
{
StrongInt len (100);
int *heapInts = new int [len];
}
gcc 4.8.2:
./StrongTypeTest.cpp: In function ‘int main()’:
./StrongTypeTest.cpp:8:30: error: ambiguous default type conversion from ‘StrongInt’
int *heapInts = new int [len];
^
./StrongTypeTest.cpp:8:30: error: candidate conversions include ‘StrongInt::operator const int&() const’ and ‘StrongInt::operator int&()’

reading struct -- uint32 does not name a type

#include <iostream>
#include <map>
#include <zlib.h>
#include <vector>
#include <stdint.h>
using namespace std;
int main (int argc, char **argv)
{
if(argc <2){ exit(0);}
//map<int, int> myMap;
struct last_touch
{
vector<uint64> baz;
uint32 foo;
uint32 bar;
}myLastTouch;
gzFile m_fHandle;
m_fHandle = gzopen(argv[1], "rb");
while(!gzeof(m_fHandle))
{
gzread(m_fHandle,&myLastTouch, sizeof(last_touch));
vector<uint64>::size_type sz = myLastTouch.baz.size();
cout<<"size \t"<<sz<<endl;
}
gzclose(m_fHandle);
}
I'm trying to read a struct from a compressed file.
and I compile it using g++ -lz test.cpp
In function ‘int main(int, char**)’:
test.cpp:15: error: ‘uint64’ was not declared in this scope
test.cpp:15: error: template argument 1 is invalid
test.cpp:15: error: template argument 2 is invalid
test.cpp:16: error: ‘uint32’ does not name a type
test.cpp:17: error: ‘uint32’ does not name a type
test.cpp:27: error: ‘uint64’ cannot appear in a constant-expression
test.cpp:27: error: template argument 1 is invalid
test.cpp:27: error: template argument 2 is invalid
test.cpp:27: error: expected initializer before ‘sz’
These are the following errors I get. I think uint32 is because of <stdint.h>and therefore i included it.
Is there somethign else that i'm missing
Those types should be with _t postfix: uint64_t

CppCMS tutorial: Linking template statically error (controller issue)

From http://cppcms.com/wikipp/en/page/cppcms_1x_tut_hello_templates#The.controller
I've places below codes on bottom of hello.cpp:
virtual void main(std::string /*url*/)
{
content::message c;
c.text=">>>Hello<<<";
render("message",c);
}
When running g++ hello.cpp my_skin.cpp -o hello -lcppcms -lbooster, got error:
hello.cpp:44:38: error: ‘virtual’ outside class declaration
hello.cpp:44:38: error: ‘::main’ must return ‘int’
hello.cpp:44:14: warning: first argument of ‘int main(std::string)’ should be ‘int’ [-Wmain]
hello.cpp:44:14: warning: ‘int main(std::string)’ takes only zero or two arguments [-Wmain]
hello.cpp: In function ‘int main(std::string)’:
hello.cpp:44:38: error: declaration of C function ‘int main(std::string)’ conflicts with
hello.cpp:27:5: error: previous declaration ‘int main(int, char**)’ here
hello.cpp: In function ‘int main(std::string)’:
hello.cpp:48:23: error: ‘render’ was not declared in this scope
Do I missed something
The error messages are telling you everything you need to know.
virtual can only be used in a class. Your main method is not in a class.
The main method must return an int. Yours is returning void.
You have two main methods, one that is main(std::string) and one that is main(int, char**)
Your render method must have a function prototype above the main method or the entire method needs to me moved.
So this would be more appropriate:
void render(std::string, std::string) // or whatever
{
// do something
}
int main(int argc, char** argv)
{
render("string", c);
return 0;
}
Your hello.cpp should look like below:
#include <cppcms/application.h>
#include <cppcms/applications_pool.h>
#include <cppcms/service.h>
#include <cppcms/http_response.h>
#include <iostream>
#include "content.h"
class hello : public cppcms::application {
public:
hello(cppcms::service &srv) : cppcms::application(srv) {}
virtual void main(std::string url);
};
void hello::main(std::string /*url*/){
content::message cc;
cc.text=">>>Hello<<<";
render("message", cc);
}
int main(int argc,char ** argv){
try {
cppcms::service srv(argc,argv);
srv.applications_pool().mount(cppcms::applications_factory<hello>());
srv.run();
}
catch(std::exception const &e) {
std::cerr<<e.what()<<std::endl;
}
}

"invalid conversion from" with pthread_create issue

Any comment is appreciated for the compile error below.
Although my question is similar to other thread: pthread function from a class, I still haven't been able to solve my problem. I am still not that familliar with pointer, and thread programming in C & C++.
Error
../src/Main.cpp: In function ‘int main(int, char**)’:
../src/Main.cpp:22: error: invalid conversion from ‘unsigned int* (*)(void*)’ to ‘void* (*)(void*)’
../src/Main.cpp:22: error: initializing argument 3 of ‘int pthread_create(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*)’
make: *** [src/Main.o] Error 1
Main.cpp
#include <process.h>
#include "ThreadInstance.hpp"
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char** argv)
{
pthread_mutex_t mutex;
int ht1;
pthread_t threadId1;
pthread_attr_t attr1;
pthread_mutex_init(&mutex, NULL);
pthread_attr_init(&attr1);
pthread_attr_setdetachstate(&attr1, PTHREAD_CREATE_DETACHED);
ht1 = pthread_create(&threadId1,
&attr1,
&ThreadInstance::ThreadEntryPoint,
//(void *)readThread);
NULL);
unsigned long rc = 0;
rc = pthread_join(ht1, NULL);
return 0;
}
ThreadInstance.hpp
#ifndef _SCENE_CLASSIFY_THREAD_H
#define _SCENE_CLASSIFY_THREAD_H
#ifndef STDCALL
#define STDCALL __attribute__((stdcall))
#endif
using namespace std;
class ThreadInstance
{
public:
ThreadInstance();
ThreadInstance(int camNum);
void startUp();
static unsigned STDCALL* ThreadEntryPoint(void* pThis)
{
//static unsigned __stdcall ThreadEntryPoint(void* pThis) {
ThreadInstance *ptr = (ThreadInstance*) pThis;
ptr->startUp();
//return 1; // Returns error "invalid conversion from ‘int’ to ‘unsigned int*’" when the function is declared as pointer.
// Since returning either 1 or 0,
// compile error still occurs.
// So this return value should not be the cause.
return 0;
}
~ThreadInstance();
};
#endif
Note: Only necessary part is shown
Your ThreadEntryPoint must return void*.
The error indicates the type that is expected, and that is the function pointer type that you are required to use.
The start function returns void* and takes void*. Yours returns unsigned int*.
Change the method to return a pointer to void.