T
Taran
Hi All,
I was trying some code which essentially does what the function 'func'
here does. To simplify the things and make a consise post I have
created a dummy app to show my problem.
The issue was to assign a value to an array of pointers. The selection
of array depends upon a condition and the void* ptr has to be casted to
the correct type and inserted in the array.
//file1.cpp
void * obj_arr;
class class_A
{
};
class class_B
{
};
class_A *a_arr[5] ;
class_B *b_arr[7];
class_A *a_obj ;
class_B *b_obj;
void func(bool is_class_A, void* void_ptr, void* new_ptr, int at_index)
{
is_class_A? \
(*(reinterpret_cast<class_A*>(void_ptr) + at_index) =
(reinterpret_cast<class_A*>(new_ptr))) : //line 1
(*(reinterpret_cast<class_B*>(void_ptr) + at_index) =
(reinterpret_cast<class_B*>(new_ptr))); // line 2
}
int main()
{
obj_arr = reinterpret_cast<void*>(a_arr);
void* new_ptr = reinterpret_cast<void*>(a_obj);
func(true, obj_arr, new_ptr,0);
obj_arr = reinterpret_cast<void*>(b_arr);
new_ptr = reinterpret_cast<void*>(b_obj);
func(false, obj_arr,new_ptr,1);
return 0;
}
Errors:
Compiling...
file1.cpp
file1.cpp(21) : error C2679: binary '=' : no operator defined which
takes a right-hand operand of type 'class class_A *' (or there is no
acceptable conversion)
file1.cpp(22) : error C2679: binary '=' : no operator defined which
takes a right-hand operand of type 'class class_B *' (or there is no
acceptable conversion)
Error executing cl.exe.
sample.exe - 2 error(s), 0 warning(s)
I have thought about the error and cannot find a problem with the cast
in the 'func'.
And specially the error thrown, both side are of correct type, pointers
to the correct class, so assignment shouldn't be any problem.
What I am doing wrong here? Am I missing something?
Any help or pointers will be appreciated.
Thanks in Advance.
I was trying some code which essentially does what the function 'func'
here does. To simplify the things and make a consise post I have
created a dummy app to show my problem.
The issue was to assign a value to an array of pointers. The selection
of array depends upon a condition and the void* ptr has to be casted to
the correct type and inserted in the array.
//file1.cpp
void * obj_arr;
class class_A
{
};
class class_B
{
};
class_A *a_arr[5] ;
class_B *b_arr[7];
class_A *a_obj ;
class_B *b_obj;
void func(bool is_class_A, void* void_ptr, void* new_ptr, int at_index)
{
is_class_A? \
(*(reinterpret_cast<class_A*>(void_ptr) + at_index) =
(reinterpret_cast<class_A*>(new_ptr))) : //line 1
(*(reinterpret_cast<class_B*>(void_ptr) + at_index) =
(reinterpret_cast<class_B*>(new_ptr))); // line 2
}
int main()
{
obj_arr = reinterpret_cast<void*>(a_arr);
void* new_ptr = reinterpret_cast<void*>(a_obj);
func(true, obj_arr, new_ptr,0);
obj_arr = reinterpret_cast<void*>(b_arr);
new_ptr = reinterpret_cast<void*>(b_obj);
func(false, obj_arr,new_ptr,1);
return 0;
}
Errors:
Compiling...
file1.cpp
file1.cpp(21) : error C2679: binary '=' : no operator defined which
takes a right-hand operand of type 'class class_A *' (or there is no
acceptable conversion)
file1.cpp(22) : error C2679: binary '=' : no operator defined which
takes a right-hand operand of type 'class class_B *' (or there is no
acceptable conversion)
Error executing cl.exe.
sample.exe - 2 error(s), 0 warning(s)
I have thought about the error and cannot find a problem with the cast
in the 'func'.
And specially the error thrown, both side are of correct type, pointers
to the correct class, so assignment shouldn't be any problem.
What I am doing wrong here? Am I missing something?
Any help or pointers will be appreciated.
Thanks in Advance.