Soldier Soldier - 2 months ago 7
C++ Question

How to create a byte array in C++?

Please have a look at the followng header file

#pragma once

class MissileLauncher
{
public:
MissileLauncher(void);

private:
byte abc[3];
};


This generated the error

Error 1 error C2143: syntax error : missing ';' before '*'


I tried to do it in this way

byte *abc;


but it also failed, same error. However, I noticed I can call other built in tyes arrays in this way for an example, an int array. Why this is happening to byte array? How to solve this? I would like to assign the values in cpp file. Any ideas?

Answer

Try

class MissileLauncher
{
public:
    MissileLauncher(void);

private:
    unsigned char abc[3];
};

or

using byte = unsigned char;

class MissileLauncher
{
public:
    MissileLauncher(void);

private:
    byte abc[3];
};

**Note: In older compilers (non-C++11) replace the using line with typedef unsigned char byte;

Comments