I have casting problem with structures in my C++ code. I use C-style casting. But if i try to cast using alternative name (created using typedef) i have an error. Please, look at my code.
class T
{
public:
typedef struct kIdS* kIdN;
typedef struct kIdS {
int* a;
double* b;
} kIdR;
typedef struct tIdS {
int* a;
double* b;
float* c;
} tIdR;
int a;
double b;
float c;
void F()
{
a = 0;
b = 0;
c = 0;
struct kIdS A = {&a, &b};
struct tIdS B = {&a, &b, &c};
struct kIdS* knv[] = {&A, (struct kIdS*)&B};
struct kIdS* knv1[] = {&A, (kIdN)&B}; //error
}
};
The error is:
error C2440: 'initializing' : cannot convert from 'T::kIdN' to 'T::kIdS *'
Types pointed to are unrelated; conversion requires reinterpret_cast,
C-style cast or function-style cast
Why i can not use alternative name? How i can solve this problem using alternative name created by typedef?
The reason for your error is in first
typedef struct kIdS* kIdNyou have no struct in your class that namedkIdS, so C++ compiler think that you are talking about a globalstruct kIdSand thentypedefthat global struct tokIdNand in linestruct kIdS* knv1[] = {&A, (kIdN)&B};error is obvious, globalstruct kIdS*can’t be used instead ofT::kIdS*, but I have a question from you, why you usestruct Xwhile you can simply sayX??