Friday, March 18, 2011

multiple definition of

You get an error such as
multiple definition of `MyClass::MyVariable'

Most probably its a static variable which is declared as well as defined in the header file.

Defining the static variable is not a good idea, you should always define your static variables in the corresponding source file.

error: invalid conversion from void (*)(void*, void*) to void*

If you get an error like "{error: invalid conversion from void (*)(void*, void*) to void*", you need to typedef your function pointer according the function being pointed to.

Consequently, use this typedefed variable in the function call instead of "void*" or something else.

In my case,
I had to call my function having the signature as

void fun(void*, void*)

My typedef would look like

typedef void (className::*Method) (void*, void*);
OR
typedef void (*Method) (void*, void*);
whichever works for you.
For the first usage you will need a forward decalartion of your class, like
class className;

In the function which takes this function pointer, use the Method variable instead of "void *" or anything else.

void callingFunc(Method function);

Hope the clarifies to some extent

Friday, February 18, 2011

getting file descriptor from file stream in linux

#include "iostream"
#include "ext/stdio_filebuf.h"
using namespace std;

int main()
{
ofstream *stream = new ofstream;
stream->open("test_file",ios::app);

typedef std::basic_filebuf filebuf_t;
filebuf_t* bbuf = dynamic_cast(stream->rdbuf());
if (bbuf != NULL) {
struct my_filebuf : public std::basic_filebuf {
int fd() { return this->_M_file.fd(); }
};

cout << "fd = " << static_cast(bbuf)->fd()<< endl;
}
}

Thursday, January 20, 2011

error: invalid initialization of non-const reference of type <> from a temporary of type

Solution: Don’t ever pass temporary object by reference.
Reason: C++ doesn't allow to bind the temporary object with non-const reference.

This is not about performance, but about the fact that you do or may alter the original, which you are not allowed to if that original is a temporary. Passing by value instead of by reference, we wouldn't have the problem.

Tuesday, January 18, 2011

error: pasting "." and "x" does not give a valid preprocessing token

Make sure you are not doing the mistake of putting "##" before macro argument i.e VAR,
it should be put only between macro argument and the string to be concatenated.

Change something like this
"arg->VAR. ## VAR ## _val,"
to "arg->VAR.VAR ## _val,"


Also, no need of "##" for the line below for calling the function.

"G(win-> ## FUNC,(c, win,"
to "G(win->FUNC,(c, win,"