utvecklare
utvecklare

Reputation: 671

class declaration error

I try to declare the following class in C++; however, I have gotten the following error. Is there something wrong with pointers?

class classFather{
public:
    int BmcCommand;
    int BmcDataLength;
    byte BmcDataBuffer[];

    classFather() {
        BmcCommand = 0;
        BmcDataLength = 0;
        BmcDataBuffer = new byte[CMD_LENGTHH];
    }

    classFather(byte s8Command, int siLength, byte as8Data[]) {
        BmcCommand = s8Command;
        BmcDataLength = siLength;
        int size = sizeof( as8Data ) / sizeof( as8Data[0] );
        BmcDataBuffer = new byte[size];
        for(int ii=0; ii< size; ii++)
            BmcDataBuffer[ii] = as8Data[ii];
    }
private:
    static const short CMD_LENGTHH = 255;
};

I'm getting the following error:

error: incompatible types in assignment of `byte*' to `byte[0u]'
C:\....\BluetoothClient\/msgCAN.h: In constructor `msgCANFather::msgCANFather(byte, int, byte*)':

Upvotes: 0

Views: 245

Answers (5)

xtofl
xtofl

Reputation: 41519

As others said, you try to assign a pointer to an array.

Better then writing memory leaks this way (I see a new but no delete), use a vector:

std::vector<byte> BmcDataBuffer;

Father(byte s8Command, int siLength, byte as8Data[]) {
    ...        
    BmcDataBuffer.insert( BmcDataBuffer.begin(), asData, asData+size );
    ...
}

Note:

int size = sizeof( as8Data ) / sizeof( as8Data[0] );

Will always return sizeof( byte* ) / sizeof( byte* ), i.e. 1.

Note 2: you could use an initializer list to create the vector member in one go:

Father(byte s8Command, int siLength, byte as8Data[]) 
: BmcDataBuffer( asData, asData+size )
{
}

the vector constructor will copy all asData elements.

Upvotes: 1

Andriy
Andriy

Reputation: 8604

In addition,

int size = sizeof( as8Data ) / sizeof( as8Data[0] );

does not what you expect. The size of a C-style array passed to a function is always unknown.

You should have an additional parameter for size or use std::vector.

Upvotes: 1

tomahh
tomahh

Reputation: 13661

by writing byte BmcDataBuffer[], you declare and array. An array is NOT a pointer, so you can not assign new byte[CMD_LENGTHH] to it. Change you declaration to byte *BmcDataBuffer will solve your compilation error.

By doing it, you need to remember to delete your newly allocated data when the object is destruct by doing something like

~classFather() {
   delete BmcDataBuffer;
}

otherwise, you would have memory leaks.

Upvotes: 2

Robᵩ
Robᵩ

Reputation: 168876

byte BmcDataBuffer[]

Change that to

byte *BmcDataBuffer;

Oh, and by the way, these lines:

classFather(byte s8Command, int siLength, byte as8Data[]) {
     int size = sizeof( as8Data ) / sizeof( as8Data[0] );

are wrong also. You can't determine the length of a passed-in array that way.

Upvotes: 2

Mene
Mene

Reputation: 3809

You're missing a ; after byte BmcDataBuffer[] and the class declaration looks wrong, too: classFather{ should be class Father { I guess.

Upvotes: 2

Related Questions