marvin_yorke
marvin_yorke

Reputation: 3559

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:(.text+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);

Upvotes: 1

Views: 214

Answers (3)

hammar
hammar

Reputation: 139890

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.

Upvotes: 0

Alexander Gessler
Alexander Gessler

Reputation: 46637

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.

Upvotes: 1

ysdx
ysdx

Reputation: 9335

You probably didn't link your .cpp file containing the constructor definition. "uint8_t" is a typedef for 'unsigned char".

Upvotes: 2

Related Questions