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,"

error: assignment of read-only location

This error/warning may come you are trying to assign value to a read-only, const variable. To overcome this issue, Always use conditional expression instead of if-else statement while assigning values to const variables.

Please check sample program.

#include

int main()
{
char *test_string = "test";
char *my_string = "my";

const char *string =
test_string?
test_string:
my_string;

/* below code will give error, don't use if-else stmt

const char *string;
if (test_string)
*string = test_string;
else
*string = my_string;
*/

printf("test_string = %s\n",test_string);
printf("my_string = %s\n",my_string);
printf("string = %s\n",string);
}

Wednesday, January 12, 2011

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

if u get the error like

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

then

Remove ## before scope resolution opeator.
For e.g change "ErrorId::##TypeId" to "ErrorId::TypeId"

Thursday, December 30, 2010

unresolved overloaded function type

error: no matching function for call to class1::input(void*, unresolved overloaded function type)
note: candidates are: int class1::input(void*, int (class1::*)(void*, int))

if your function call is like
input( (void*)&cur_msg, class1::input);

change it to
input( (void*)&cur_msg, &class1::input);

Its basically a function pointer, so you need to pass address of that function

Tuesday, December 28, 2010

error: TIOCFLUSH was not declared in this scope

include "sys/ioctl.h" on Linux, change TIOCFLUSH to TCFLSH in ioctl call

error: expected constructor, destructor, or type conversion before & token

If you get the following error
"error: expected constructor, destructor, or type conversion before & token"
or
"error: expected initializer before & token"

Add "std::" before the token, if token is part of std namespace.
like std::ostream.
You can also think of adding "using namespace std;" in your source file but Please don't add using line in your header file.

Thursday, December 23, 2010

error: extra qualification on member

If you get the above error, just remove the extra scope resolution operator,:: and it should work fine.

Monday, December 20, 2010

error: ostrstream was not declared in this scope

If you get such errors
"error: ostrstream was not declared in this scope"

try to include its relevant header file and/or namespace.

For this error, include or along with using namespace std;

Saturday, December 18, 2010

Yacc and Lex

In Linux,
Yacc is a script which calls Bison (Yacc supported utility on Linux).
Lex is a script which calls Flex (Lex supported utility on Linux).

If you face linking issue for lex, please change "-ll" to "-lfl". Yacc should work with "-ly" as usual.

If you get following error,
"error: macro "yywrap" passed 1 arguments, but takes just 0"

Change "#define yywrap() 1" to "%option noyywrap".
Note put this option line outside of %{ ...}% section. This section is for the code which needs to get directly into the generated .c file.

Wednesday, December 15, 2010

Compiler Flags

cc/CC Flags on Solaris------>gcc/g++ Flags on Linux
===================================================
"-fast" ------> "-O2"
"-O5" ------> "-O3"
"-xarch" ------> "-mcpu, -march"
"-xchip" ------> No Equivalent in Linux (probably not required)
"-PIC/KPIC" -----> "-fPIC"
"-library" --------> "-I Include Path -L Lib Path"
"-compat=5" -------> "-fabi-version=2"
"-D__SUNPRO_C" ------> "__VERSION__"
"-ptr" -------------> No equivalent in Linux, not required.
"-ll" -------------> "-lfl" - For linking lex, as Linux has Flex