18

Please have a look at the following 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;

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

2
  • 2
    There's no * in the code you present, so it can't possibly generate that error message. Please be accurate. Commented May 10, 2013 at 19:22
  • 2
    Also, where is byte coming from? It's not a standard type. Commented May 10, 2013 at 19:22

6 Answers 6

34

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;

Sign up to request clarification or add additional context in comments.

4 Comments

char need not to be one byte
char is guaranteed to be one byte; sizeof(char) == 1, always. But "byte" is not guaranteed to be 8 bits.
@Nicolás sizeof returns size in chars. The only guarantee the standard makes is that sizeof(char) is 1. The rest are undefined behavior. A byte is not guaranteed to be 1 char. POSIX 1-2001 seems to define CHAR_BIT macro to be 8. See: gnu.org/software/libc/manual/html_node/Width-of-Type.html
@Dragas sizeof returns a byte count, a byte may be more than 8 bits en.cppreference.com/w/c/language/sizeof
15

If you want exactly one byte, uint8_t defined in cstdint would be the most expressive.

http://www.cplusplus.com/reference/cstdint/

3 Comments

Also, C++11 only it seems.
@MichaelPrice, no, it's available in previous C++s as well. Use <stdint.h> instead of <cstdint>. I'm using it today in some code as we speak. :-)
@KellyBeard - <cstdint> is only available after C++11 was shipped (as a standard, portable method). <cstdint> and <stdint.h> are not exactly the same thing.
9

Maybe you can leverage the std::bitset type available in C++11. It can be used to represent a fixed sequence of N bits, which can be manipulated by conventional logic.

#include<iostream>
#include<bitset>

class MissileLauncher {
 public:
  MissileLauncher() {}
  void show_bits() const {
    std::cout<<m_abc[2]<<", "<<m_abc[1]<<", "<<m_abc[0]<<std::endl;
  }

  bool toggle_a() {
    // toggles (i.e., flips) the value of `a` bit and returns the
    // resulting logical value
    m_abc[0].flip();
    return m_abc[0];
  }

  bool toggle_c() {
    // toggles (i.e., flips) the value of `c` bit and returns the
    // resulting logical value
    m_abc[2].flip();
    return m_abc[2];
  }

  bool matches(const std::bitset<3>& mask) {
    // tests whether all the bits specified in `mask` are turned on in
    // this instance's bitfield
    return ((m_abc & mask) == mask);
  }

 private:
  std::bitset<3> m_abc;
};

typedef std::bitset<3> Mask;
int main() {
  MissileLauncher ml;

  // notice that the bitset can be "built" from a string - this masks
  // can be made available as constants to test whether certain bits
  // or bit combinations are "on" or "off"
  Mask has_a("001");       // the zeroth bit
  Mask has_b("010");       // the first bit
  Mask has_c("100");       // the second bit
  Mask has_a_and_c("101"); // zeroth and second bits
  Mask has_all_on("111");  // all on!
  Mask has_all_off("000"); // all off!

  // I can even create masks using standard logic (in this case I use
  // the or "|" operator)
  Mask has_a_and_b = has_a | has_b;
  std::cout<<"This should be 011: "<<has_a_and_b<<std::endl;

  // print "true" and "false" instead of "1" and "0"
  std::cout<<std::boolalpha;

  std::cout<<"Bits, as created"<<std::endl;
  ml.show_bits();
  std::cout<<"is a turned on? "<<ml.matches(has_a)<<std::endl;
  std::cout<<"I will toggle a"<<std::endl;
  ml.toggle_a();
  std::cout<<"Resulting bits:"<<std::endl;
  ml.show_bits();  
  std::cout<<"is a turned on now? "<<ml.matches(has_a)<<std::endl;
  std::cout<<"are both a and c on? "<<ml.matches(has_a_and_c)<<std::endl;
  std::cout<<"Toggle c"<<std::endl;
  ml.toggle_c();
  std::cout<<"Resulting bits:"<<std::endl;
  ml.show_bits();    
  std::cout<<"are both a and c on now? "<<ml.matches(has_a_and_c)<<std::endl;  
  std::cout<<"but, are all bits on? "<<ml.matches(has_all_on)<<std::endl;
  return 0;
}

Compiling using gcc 4.7.2

g++ example.cpp -std=c++11

I get:

This should be 011: 011
Bits, as created
false, false, false
is a turned on? false
I will toggle a
Resulting bits:
false, false, true
is a turned on now? true
are both a and c on? false
Toggle c
Resulting bits:
true, false, true
are both a and c on now? true
but, are all bits on? false

1 Comment

The question was a byte array, not a bit array
7

Byte is not a standard type in C or C++. Try char, which is usually and at least 8 bits long.

2 Comments

Thanks for the reply. So, using char array? Can it send values into USB ports just as bytes?\
When you use a char it's exactly one byte; so send 'char' to USB port in C / C++ you are actually sending a byte.
2

Byte is not a standard data type in C/C++ but it can still be used the way i suppose you want it. Here is how: Recall that a byte is an eight bit memory size which can represent any of the integers between -128 and 127, inclusive. (There are 256 integers in that range; eight bits can represent 256 -- two raised to the power eight -- different values.). Also recall that a char in C/C++ is one byte (eight bits). So, all you need to do to have a byte data type in C/C++ is to put this code at the top of your source file: #define byte char So you can now declare byte abc[3];

Comments

1

You could use Qt which, in case you don't know, is C++ with a bunch of additional libraries and classes and whatnot. Qt has a very convenient QByteArray class which I'm quite sure would suit your needs.

http://qt-project.org/

1 Comment

this is like javascript's "just use jquery" but for c++.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.