Improper typecasting in Class constructor
I'm new to C++ and I have a problem with classes.
I got this prototype
class MMA7455 : public Accel
{
public:
MMA7455(uint8_t);
uint8_t accel_get_data(acceleration_t*);
private:
uint8_t accel_data_ready(void);
};
and I want to create an instance of it
MMA7455 accel = MMA7455(0x21);
but the following message appears
In function `global constructors keyed to accel':
sensors.cpp:(.te开发者_如何学JAVAxt+0x8): undefined reference to `MMA7455::MMA7455(unsigned char)'
Why it's looking for 'unsigned char' argument? Same message even if I try to implicitly cast the type of constant
MMA7455 accel = MMA7455((uint8_t)0x21);
You probably didn't link your .cpp file containing the constructor definition. "uint8_t" is a typedef for 'unsigned char".
You need to define MMA7455::MMA7455(uint8_t)
somewhere in your program, i.e. add a {}
-body after the definition in the prototype (or perhaps you just forgot to compile and link the cpp-file containing the definitions for MMA7455
.
It looks for unsigned char
because uint8_t
happens to be a typedef
for unsigned char
on your system.
uint8_t
is a typedef for unsigned char
on your platform. The error is a linker error since you haven't provided an implementation for your constructor and is unrelated to the argument being an unsigned char
or not.
精彩评论