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"