0

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*)':
John Dibling
  • 94,084
  • 27
  • 171
  • 303
utvecklare
  • 661
  • 1
  • 11
  • 25
  • Arrays are not pointers. – chris Oct 24 '12 at 14:08
  • 2
    You might as well use a vector instead. You already have a memory leak. – chris Oct 24 '12 at 14:10
  • The error message is pretty clear: incompatible types in assignment. – jogojapan Oct 24 '12 at 14:13
  • -1 not real code. seriously, what are you *thinking* of when you ask for help with a syntax issue in your real code, and post imaginary code that's full of other issues? **we are not telepaths**. – Cheers and hth. - Alf Oct 24 '12 at 14:52
  • You don't know that. Some of us might be telepaths – jalf Oct 24 '12 at 14:58
  • @Cheersandhth.-Alf Should I upload the whole project that C++ classes are just one part of it? I had misunderstanding on this part, so I decided to write about this part, and unfortunately forgot one semicolon. That's it. – utvecklare Oct 24 '12 at 16:09
  • 2
    @utvecklare - You should *start* with your whole project, then reduce your original program to the smallest possible actual complete program that, when compiled, still produces the error. In your case, that would have been a five-line program. Sometimes we call this process SSCCE (or Short Self-Contained Complete Example) after the website that explains the process: http://SSCCE.ORG/. Such a minimal program might look like this: http://ideone.com/WlMAIQ – Robᵩ Oct 24 '12 at 17:42
  • @Cheersandhth.-Alf It seems that you're searching for something especial for yourself by posing such comments. You are so proud of yourself in a way that you have forgotten you are trying to HELP others like me with less knowledge here in stackoverflow. Although I believe that I have made mistakes by my typing, you could at least mention it in other ways instead of calling yourself a telepath or calling me a liar. Good Luck buddy. – utvecklare Oct 24 '12 at 18:12
  • 1
    @utvecklare: Alf's right, though; StackOverflow is a community of trust. It's a newcomer's mistake to not recognise that - no offense. It really helps if your question shows that you did the best you could to solve your own problem, and honestly speaking, my initial idea was "no way the compiler gotten that far?". But as you may find, the people here are really willing to help, but are not afraid to speak up when they feel offended. And that's a really good thing. – xtofl Oct 24 '12 at 20:06
  • 1
    On the other hand, you stayed really polite in your comment reply :) – xtofl Oct 24 '12 at 20:07

5 Answers5

2

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

Mene
  • 3,449
  • 18
  • 34
2
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.

Robᵩ
  • 143,876
  • 16
  • 205
  • 276
2

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.

tomahh
  • 12,429
  • 3
  • 44
  • 65
  • 1
    Don't forget the rule of three/five and all it's joys as well. I stick with the rule of zero. – chris Oct 24 '12 at 14:11
  • @chris I dont understand what you mean at all. – tomahh Oct 24 '12 at 14:12
  • [What is the Rule of Three?](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) [Rule of Four/Five](http://stackoverflow.com/questions/4782757/rule-of-three-becomes-rule-of-five-with-c11). [Rule of Zero](http://rmartinho.github.com/2012/08/15/rule-of-zero.html). On another note, how did I manage to use it's instead of its? – chris Oct 24 '12 at 14:15
1

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.

Andrey
  • 8,156
  • 3
  • 23
  • 50
1

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.

xtofl
  • 38,207
  • 10
  • 95
  • 177
  • Does it need to new the vector in constructors and delete it in Vector? – utvecklare Oct 24 '12 at 14:19
  • 1
    `vector` takes care of all new and delete for you. Efficiently, safely and written by the best developers in the world. – xtofl Oct 24 '12 at 14:22
  • One more question ;) I have a buffer `char myIncomingBiffer[myBufferSize];` which is filled by incoming Bluetooth messages. I process it and then one part of it which is `as8Data[]` will be passes to the constructor of Father class to shape a message object. Can I declare the first buffer which is listening to the Bluetooth messages as a vector? The thing is that as far as I understand it is not required to assign the size for vector. So what will happen in this case that it should pass its content regularly to the processing section since the end of vector is not defined? – utvecklare Oct 24 '12 at 19:34
  • 1
    That's a different question, really. Short answer: Yes. Long answer: `vector` is especially designed to handle 'unkown upfront' buffer sizes. It _can_ handle fixed sizes, of course, and it's generally considered as your first choice container. `vector::resize(size_t)` will allocate value-initialized data, and e.g. `& vector[0]` converts the `vector` to a raw `byte` pointer. BUT DO take a look at the documentation! http://en.cppreference.com/w/cpp/container/vector is a good place to start reading. – xtofl Oct 24 '12 at 20:00