我正在尝试使用变量掩码作为指针,可以在下面的多个函数中重复使用。它适用于第一个函数,它只是“mask = (unsigned short*)0;”关闭所有位。现在我正在使用按位运算符,我无法让该函数在两种指针类型上工作。 是否有可能在非类对象指针上创建运算符重载函数?
如果是这样我做错了什么?
unsigned short mask operator << (unsigned short *mask, int *nbits) {
mask <<= nbits;
return mask;
}
//I expected the function above to work as operator overloading function like I practiced in c++ on class objects.
unsigned short center_Lightson(unsigned short *mask, int*nbits, int*sbit, unsigned short *wrd)
{
mask = (unsigned short*)~0;
mask <<= nbits;
mask = ~mask;
mask <<= sbit;
mask = ~mask;
wrd = mask;
printbits(wrd);
return mask;
}
unsigned short left_Lightson(unsigned short*mask, int*nbits, int*sbit, unsigned short* wrd) {
*mask |= (~((unsigned short)~0 << *nbits)) << *sbit;
wrd = mask;
printbits(wrd);
return mask;
}
compiler warnings found:
Severity Code Description Project File Line Suppression State
Error C2296 '<<=': illegal, left operand has type 'unsigned short *' Project17 C:\Users\Student\Downloads\26B_H_4\26B_H_4B.c 201
Error C2297 '<<=': illegal, right operand has type 'int *' Project17 C:\Users\Student\Downloads\26B_H_4\26B_H_4B.c 201
Error C2171 '~': illegal on operands of type 'unsigned short *' Project17 C:\Users\Student\Downloads\26B_H_4\26B_H_4B.c 202
Error C2296 '<<=': illegal, left operand has type 'unsigned short *' Project17 C:\Users\Student\Downloads\26B_H_4\26B_H_4B.c 203
Error C2297 '<<=': illegal, right operand has type 'int *' Project17 C:\Users\Student\Downloads\26B_H_4\26B_H_4B.c 203
Error C2171 '~': illegal on operands of type 'unsigned short *' Project17 C:\Users\Student\Downloads\26B_H_4\26B_H_4B.c 204 `