Seb
Seb

Reputation: 3215

redefinition of a class

I have defined in my header file the class below:

class mtp_wrapper
{
private:
    LIBMTP_raw_device_t * usbrawdevice;
    int numusbrawdevice;
    LIBMTP_error_number_t err;
    LIBMTP_mtpdevice_t *dev;

public:
    mtp_wrapper() {
        dev = NULL;
    };
    void setDevice(LIBMTP_mtpdevice_t *dev);
    LIBMTP_mtpdevice_t *getDevice();
};

and in the c++ part is :

mtp_wrapper::mtp_wrapper()
{
 .... <some code>
} 


void mtp_wrapper::setDevice(LIBMTP_mtpdevice_t *dev) {
    this->dev = dev;
}

LIBMTP_mtpdevice_t * mtp_wrapper::getDevice() {
    return dev;
}

By the way, when building I got an issue saying that mtp_wrapper is already defined but I need to make sure that the dev variable is initialised at NULL.

In 99% of examples, I saw people using this and it seems working in their cases

Upvotes: 0

Views: 92

Answers (5)

Vazquinhos
Vazquinhos

Reputation: 132

Try this in the .h file

class mtp_wrapper
{
private:
    LIBMTP_raw_device_t * usbrawdevice;
    int numusbrawdevice;
    LIBMTP_error_number_t err;
    LIBMTP_mtpdevice_t *dev;

public:
    mtp_wrapper();
    void setDevice(LIBMTP_mtpdevice_t *dev);
    LIBMTP_mtpdevice_t *getDevice();
};

and this in the cpp

mtp_wrapper::mtp_wrapper() : dev(NULL)
{
 .... <some code>
} 


void mtp_wrapper::setDevice(LIBMTP_mtpdevice_t *dev) {
    this->dev = dev;
}

LIBMTP_mtpdevice_t * mtp_wrapper::getDevice() {
    return dev;
}

You have defined twice the constructor, once in the header and once in the cpp. However if you want to make sure that the def variable is NULL put the : dev( NULL )

Hope it works!

Upvotes: 2

ravi
ravi

Reputation: 10733

This is first definition in .h file

mtp_wrapper() {
    dev = NULL;
};

and below one is second definition in .cpp file:-

mtp_wrapper::mtp_wrapper()
{
 .... <some code>
} 

for which compiler is crying

Upvotes: 0

Anton Savin
Anton Savin

Reputation: 41301

You are getting the error message because you defined the default constructor here:

mtp_wrapper() {
    dev = NULL;
};

and here again:

mtp_wrapper::mtp_wrapper()
{
 .... <some code>
}

Upvotes: 0

Ali Kazmi
Ali Kazmi

Reputation: 1458

mtp_wrapper() is defined in the header file and you are trying to define it in implementation file (.cpp) file. Quick Fix : declare mtp_wrapper() in header as mtp_wrapper();

Upvotes: 0

vahancho
vahancho

Reputation: 21230

The problem is that you defined your mtp_wrapper() constructor twice: once in the header file and second time in your cpp file. Just merge both implementations and write in the .cpp file:

mtp_wrapper::mtp_wrapper()
{
    dev = NULL;
 .... <some code>
} 

Upvotes: 1

Related Questions